C++ Primer(第五版) 第十章练习答案

C++ Primer(第五版) 第十章练习答案

目录

  • C++ Primer(第五版) 第十章练习答案
      • 10.1
      • 10.2
      • 10.3
      • 10.4
      • 10.5
      • 10.6
      • 10.7
      • 10.8
      • 10.9
      • 10.10
      • 10.11
      • 10.12
      • 10.13
      • 10.14
      • 10.15
      • 10.16
      • 10.17
      • 10.18
      • 10.19
      • 10.20
      • 10.21
      • 10.22
      • 10.23
      • 10.24
      • 10.25
      • 10.26
      • 10.27
      • 10.28
      • 10.29
      • 10.30
      • 10.31
      • 10.32
      • 10.33
      • 10.34
      • 10.35
      • 10.36
      • 10.37
      • 10.38
      • 10.39
      • 10.40
      • 10.41
      • 10.42

10.1

#include 
#include 
#include 

using namespace std;

int main()
{
    vector<int> vec;
    int i;

    while (cin >> i)
        vec.emplace_back(i);

    cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);

    while (getchar() != '\n');

    cin >> i;

    cout << count(vec.begin(), vec.end(), i) << endl;

    return 0;
}

10.2

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    list<string> lst;
    string s;

    while (cin >> s)
        lst.emplace_back(s);

    cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);

    while (getchar() != '\n');

    cin >> s;

    cout << count(lst.begin(), lst.end(), s) << endl;

    return 0;
}

10.3

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    vector<int> vec(3, 10);

    auto sum = accumulate(vec.cbegin(), vec.cend(), 0);
    
    cout << sum << endl;

    return 0;
}

10.4


/**
 * double 转换成 int
 */

10.5


/**
 * 无法比较
 * 没有 == 运算符
 * 报错
 */

10.6

#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    vector<int> vec;

    fill_n(back_inserter(vec), 10, 0);

    for (auto &i : vec)
        cout << i << ends;
    cout << endl;

    return 0;
}

10.7



/**
 * a
 * copy(lst.cbegin(), lst.cend(), back_inserter(vec));
 * 
 * b
 * fill_n(back_inserter(vec), 10, 0);
 */

10.8



/**
 * 对 back_inserter 赋值时 右边的值被添加到容器中
 * 迭代器赋的值关我算法什么事
 */

10.9

#include 
#include 
#include 
#include 
#include 

using namespace std;

void elimDups(vector<string> &vec);

int main()
{
    vector<string> vec;
    string str;

    while (cin >> str)
        vec.emplace(vec.end(), str);

    elimDups(vec);
    
    for (auto &i : vec)
        cout << i << ends;
    cout << endl;


    return 0;
}

void elimDups(vector<string> &vec)
{
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

10.10



/**
 * 迭代器没有这操作
 */

10.11

#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef bool(*is)(const string&, const string&);
typedef void(*st_sort)(vector<string>::iterator, vector<string>::iterator, is);

bool isShorter(const string &s1, const string &s2);
void elimDups(vector<string> &vec, st_sort, is);

int main()
{
    vector<string> vec;
    string str;

    while (cin >> str)
        vec.emplace(vec.end(), str);

    elimDups(vec, stable_sort, isShorter);

    for (auto &i : vec)
        cout << i << ends;
    cout << endl;

    return 0;
}

void elimDups(vector<string> &vec, st_sort u, is i)
{
    // 按字典排序
    sort(vec.begin(), vec.end());
    // 删除相同元素
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    // 按长度重排, 长度相同维持字典序
    u(vec.begin(), vec.end(), i);
}

bool isShorter(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}

10.12

#include 
#include 
#include 
#include 
#include 
#include "../Sales_data/Sales_data.h"
// #include "../Sales_data/Sales_data.cc"

using namespace std;

bool compareIsbn(const Sales_data &s1, const Sales_data &s2);

int main()
{
    vector<Sales_data> vec;
    Sales_data sa;
    while (read(cin, sa))
    {
        vec.push_back(sa);
    }

    sort(vec.begin(), vec.end(), compareIsbn);

    for (auto &i : vec)
        print(cout, i) << endl;

    return 0;
}

bool compareIsbn(const Sales_data &s1, const Sales_data &s2)
{
    return s1.isbn() < s2.isbn();
}

10.13

#include 
#include 
#include 
#include 
#include 

using namespace std;

bool is(const string &s);

int main()
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.emplace_back(str);

    auto par_end = partition(vec.begin(), vec.end(), is);

    while (par_end-- != vec.begin())
        cout << *par_end << endl;

    return 0;
}

bool is(const string &s)
{
    return s.size() >= 5;
}

10.14


/**
 * 
 *  auto f = [](int a, int b) -> int { return a + b; };
 */

10.15

#include 
#include 
#include 
#include 
#include 

using namespace std;

auto main() -> int
{
    int a = 10;
    auto f = [a](int b) -> int { return a + b; };

    cout << f(10) << endl;

    return 0;
}

10.16

#include 
#include 
#include 
#include 
#include 

using namespace std;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // 长度大于 sz 的第一个元素
    auto wc = find_if(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
    // 大于 sz 的数量
    auto count = words.end() - wc;
    // 输出
    cout << count << endl;
    for_each(wc, words.end(), [](const string &s) { cout << s << ends; });
    cout << endl;
}

10.17

#include 
#include 
#include 
#include 
#include 
#include "../Sales_data/Sales_data.h"
// #include "../Sales_data/Sales_data.cc"

using namespace std;

int main()
{
    vector<Sales_data> vec;
    Sales_data sa;
    while (read(cin, sa))
    {
        vec.push_back(sa);
    }

    sort(vec.begin(), vec.end(), [](const Sales_data &s, const Sales_data &b){ return s.isbn() < b.isbn(); });

    for (auto &i : vec)
        print(cout, i) << endl;

    return 0;
}

10.18

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // tr 存入 条件为 true 的元素
    vector<string> tr;
    vector<string> fa;
    partition_copy(words.begin(), words.end(), back_inserter(tr), back_inserter(fa), [sz](const string &s) { return s.size() >= sz; });
    // 大于 sz 的数量
    auto count = tr.size();
    // 输出
    cout << count << endl;
    for_each(tr.begin(), tr.end(), [](const string &s) { cout << s << ends; });
    cout << endl;
}

10.19

#include 
#include 
#include 
#include 
#include 

using namespace std;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // 长度大于 sz 的尾后元素
    auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
    // 大于 sz 的数量
    auto count = wc - words.begin();
    // 输出
    cout << count << endl;
    for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
    cout << endl;
}

10.20

#include 
#include 
#include 
#include 
#include 

using namespace std;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // 长度大于 sz 的尾后元素
    auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
    // 大于 sz 的数量
    auto count = wc - words.begin();
    auto count6 = count_if(words.cbegin(), words.cend(), [](const string &s) { return s.size() > 6; });
    // 输出
    cout << count << endl;
    cout << count6 << endl;
    for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
    cout << endl;
}

10.21

#include 
#include 
#include 
#include 
#include 

using namespace std;

auto main() -> int
{
    int a = 10;
    auto func = [&]() ->bool { if (a == 0) return true; while (a-- != 1); return true; };

    cout << a << endl;
    cout << boolalpha << func() << endl;
    cout << a << endl;
    cout << func() << noboolalpha << endl;

    return 0;
}

10.22

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace std::placeholders;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
bool check_size(const string &s, vector<string>::size_type sz);

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // 长度大于 sz 的尾后元素
    auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
    // 大于 sz 的数量
    auto count = wc - words.begin();
    auto count6 = count_if(words.cbegin(), words.cend(), bind(check_size, _1, 6));
    // 输出
    cout << count << endl;
    cout << count6 << endl;
    for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
    cout << endl;
}

bool check_size(const string &s, vector<string>::size_type sz)
{
    return s.size() >= sz;
}

10.23



/**
 * 29
 * 
 *  extern const _Placeholder<1> _1;
    extern const _Placeholder<2> _2;
    extern const _Placeholder<3> _3;
    extern const _Placeholder<4> _4;
    extern const _Placeholder<5> _5;
    extern const _Placeholder<6> _6;
    extern const _Placeholder<7> _7;
    extern const _Placeholder<8> _8;
    extern const _Placeholder<9> _9;
    extern const _Placeholder<10> _10;
    extern const _Placeholder<11> _11;
    extern const _Placeholder<12> _12;
    extern const _Placeholder<13> _13;
    extern const _Placeholder<14> _14;
    extern const _Placeholder<15> _15;
    extern const _Placeholder<16> _16;
    extern const _Placeholder<17> _17;
    extern const _Placeholder<18> _18;
    extern const _Placeholder<19> _19;
    extern const _Placeholder<20> _20;
    extern const _Placeholder<21> _21;
    extern const _Placeholder<22> _22;
    extern const _Placeholder<23> _23;
    extern const _Placeholder<24> _24;
    extern const _Placeholder<25> _25;
    extern const _Placeholder<26> _26;
    extern const _Placeholder<27> _27;
    extern const _Placeholder<28> _28;
    extern const _Placeholder<29> _29;
 */

10.24

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace std::placeholders;

bool check_size(int a, size_t sz);

auto main() -> int
{
    string s(4, 'f');
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    auto it = find_if(vec.begin(), vec.end(), bind(check_size, _1, s.size()));

    while (it != vec.end())
        cout << *it++ << endl;

    return 0;
}

bool check_size(int a, size_t sz)
{
    return a >  sz;
}

10.25

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace std::placeholders;

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
bool check_size(const string &s, vector<string>::size_type sz);

auto main() -> int
{
    vector<string> vec;
    string str;
    while (cin >> str)
        vec.push_back(str);

    biggies(vec, 3);

    return 0;
}

auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
    // 字典排序
    sort(words.begin(), words.end());
    // 删除重复元素
    words.erase(unique(words.begin(), words.end()), words.end());
    // 按长度重排, 长度相同维持字典序
    stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
    // tr 存入 条件为 true 的元素
    vector<string> tr;
    vector<string> fa;
    partition_copy(words.begin(), words.end(), back_inserter(tr), back_inserter(fa), bind(check_size, _1, sz));
    // 大于 sz 的数量
    auto count = tr.size();
    // 输出
    cout << count << endl;
    for_each(tr.begin(), tr.end(), [](const string &s) { cout << s << ends; });
    cout << endl;
}

bool check_size(const string &s, vector<string>::size_type sz)
{
    return s.size() >= sz;
}

10.26



/**
 * back_inserter  尾插
 * front_inserter 头插
 * inserter       前插
 */

10.27

#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    vector<int> vec{1, 2, 3, 3, 4, 5, 5, 1, 11, 22, 4, 3};
    list<int> lst;

    sort(vec.begin(), vec.end());
    unique_copy(vec.begin(), vec.end(), back_inserter(lst));

    for_each(lst.begin(), lst.end(), [](int &a) { cout << a << endl; });

    return 0;
}

10.28

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define foreach(a, b) {  \
    for_each(a, b, [](int x){ cout << x << ends; });  \
    cout << endl;  \
}

using namespace std;

int main()
{
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
    forward_list<int> flst;
    list<int> lst;
    vector<int> vec2;
    // queue que;  没有三种操作

    copy(vec.begin(), vec.end(), back_inserter(lst));
    copy(vec.begin(), vec.end(), front_inserter(flst));
    copy(vec.begin(), vec.end(), inserter(vec2, vec2.end()));

    foreach(flst.begin(), flst.end());
    foreach(vec2.begin(), vec2.end());
    foreach(lst.begin(), lst.end());

    return 0;
}

10.29

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))

using namespace std;

int main(int argc, char **argv)
{
    if (argc != 2) 
        puts("??"), exit(1);

    ifstream in_file(argv[1]);
    istream_iterator<string> in_iter(in_file), eof;

    vector<string> vec;

    copy(in_iter, eof, back_inserter(vec));
    foreach(vec.begin(), vec.end(), string);

    return 0;
}

10.30

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))

using namespace std;

int main()
{
    istream_iterator<int> in_iter(cin), eof;

    vector<int> vec(in_iter, eof);

    sort(vec.begin(), vec.end());
    foreach(vec.begin(), vec.end(), int);

    return 0;
}

10.31

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))

using namespace std;

int main()
{
    istream_iterator<int> in_iter(cin), eof;

    vector<int> vec(in_iter, eof);

    sort(vec.begin(), vec.end());
    unique_copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));

    return 0;
}

10.32

#include "../Sales_item/Sales_item.h"
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))

int main()
{
    istream_iterator<Sales_item> item_iter(cin), eof;
    ostream_iterator<Sales_item> out_iter(std::cout, "\n");

    vector<Sales_item> vec(item_iter, eof);
    // 排序
    sort(vec.begin(), vec.end(), [](const Sales_item &i, const Sales_item &b) { return i.isbn() < b.isbn(); });

    auto beg = vec.begin();
    // 上一个
    Sales_item sum = *beg++;

    while (beg != vec.end())
    {
        // 是否有相同
        if (find_if(beg, vec.end(), [sum](const Sales_item &i) { return i.isbn() == sum.isbn(); }) != vec.end())
        {
            // 上一个 加 本次
            sum = accumulate(beg, beg + 1, sum);
            ++beg;
        }
        else
        {
            out_iter = sum;
            sum = *beg++;
        }
    }
    // 最后一个
    out_iter = sum;

    return 0;
}

10.33

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))

int main(int argc, char **argv)
{
    if (argc != 4)
        puts("??"), exit(1);

    ifstream in_file(argv[1]);
    ofstream out_file_ou(argv[2]);
    ofstream out_file_ji(argv[3]);

    istream_iterator<int> in_iter(in_file), eof;
    ostream_iterator<int> out_iter_ou(out_file_ou, "\n");
    ostream_iterator<int> out_iter_ji(out_file_ji, " ");

    while (in_iter != eof)
    {
        if (*in_iter % 2 == 0)
            *out_iter_ou++ = *in_iter++;
        else
            *out_iter_ji++ = *in_iter++;
    }
    

    return 0;
}

10.34

#include 
#include 
#include 
#include 

using namespace std;

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))

int main()
{
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    foreach(vec.rbegin(), vec.rend(), int);

    return 0;
}

10.35

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    for (auto end = vec.end() - 1; end != vec.begin() - 1; --end)
        cout << *end << endl;

    return 0;
}

10.36

#include 
#include 
#include 
#include 
#include 

using namespace std;

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))

int main()
{
    list<int> lst{10, 2, 3, 0, 4, 5, 0, 1, 2, 3};
    auto f = find(lst.rbegin(), lst.rend(), 10);

    int index = 0;
    auto beg = lst.begin();

    while (beg != f.base())
    {
        ++index;
        ++beg;
    }

    cout << index << endl;

    return 0;
}

10.37

#include 
#include 
#include 
#include 
#include 

using namespace std;

#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))

int main()
{
    vector<int> vec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    list<int> lst;

    // reverse_copy(vec.begin() + 3, vec.begin() + 7, back_inserter(lst));
    copy(vec.rbegin() + 3, vec.rbegin() + 7, back_inserter(lst));

    foreach(lst.begin(), lst.end(), int);

    return 0;
}

10.38


/**
 * istream_iterator      输入迭代器
 * ==
 * !=
 * ++
 * *
 * ->
 * 
 * ostream_iterator      输出迭代器
 * ++
 * *
 * 
 * forward_list.begin()  向前迭代器
 * 包含上面的操作
 * 
 * list.begin()          双向迭代器
 * 包含上面的操作
 * --
 * 
 * vector.begin()        随机访问迭代器
 * 包含上面的操作
 * < <= > >=
 * + += - -=
 * 迭代器 - 迭代器
 * []
 */

10.39


/**
 * 双向迭代器
 * 随机访问迭代器
 */

10.40



/**
 * 输出迭代器
 * 双向迭代器
 * 向前迭代器
 */

10.41


/**
 * 把范围中的 old_val 替换成 new_val
 * pred 为真的元素 替换成 new_val
 * 把范围中的 old_val 替换成 new_val 后写入 dest
 * pred 为真的元素 替换成 new_val 后写入 dest
 */

10.42

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    istream_iterator<string> in_iter(cin), eof;
    ostream_iterator<string> out_iter(cout, "\12");
    list<string> lst(in_iter, eof);

    // 字典序
    lst.sort();
    // 长度序
    lst.sort([](const string &s1, const string &s2) { return s1.size() < s2.size(); });
    // 删除相同元素
    lst.unique();
    
    copy(lst.begin(), lst.end(), out_iter);

    return 0;
}

你可能感兴趣的:(C++,Primer(第五版),c++,stl,算法)