C++ Primer 10章习题

10.1

int main(int argc, char* argv[])
{
    vector<int> vectemp;
    int inttemp = 0;
    cout << "输入vec中文字" << endl;
    while (cin >> inttemp)
        vectemp.push_back(inttemp);
    cin.clear();
    cout << "输入要找的数字" << endl;
    cin >> inttemp;
    cout << "总共找到";
    cout << count(vectemp.cbegin(), vectemp.cend(), inttemp);
    cout << "次" << endl;
    return 0;
}

10.2 略
10.3

int main(int argc, char* argv[])
{
    vector<int> vectemp;
    int inttemp = 0;
    cout << "输入vec中数字" << endl;
    while (cin >> inttemp)
        vectemp.push_back(inttemp);
    cout << "和为:";
    cout << accumulate(vectemp.cbegin(), vectemp.cend(), 0) << endl;
    return 0;
}

10.4 和为int型
10.5 错误。比较指针没有意义。
10.6

int main(int argc, char* argv[])
{
    vector<int> vectemp;
    int inttemp = 0;
    cout << "输入vec中数字" << endl;
    while (cin >> inttemp)
        vectemp.push_back(inttemp);
    fill_n(vectemp.begin(), vectemp.size(), 0);
    for (const auto &c : vectemp)
        cout << c << ends;
    cout << endl;
    return 0;
}

10.7 (a)空容器
(b)仍然是空容器,reserve只分配足够内存,没有写入元素。
10.8 。。。
10.9

void stupid_elimdups(vector<string> &vec)
{
    string strtemp;
    cout << "输入vec中内容" << endl;
    while (cin >> strtemp)
        vec.push_back(strtemp);
    sort(vec.begin(), vec.end());
    cout << "初始内容为:" << endl;
    for (const auto &c : vec)
        cout << c << ends;
    cout << endl;

    auto end_unique = unique(vec.begin(), vec.end());
    cout << "使用unique之后" << endl;
    for (const auto &c : vec)
        cout << c << ends;
    cout << endl;

    vec.erase(end_unique, vec.end());
    cout << "使用erase之后" << endl;
    for (const auto &c : vec)
        cout << c << ends;
    cout << endl;
}

10.10 算法只操作迭代器。
10.11-10.12 略
10.13

bool morethanfive(const string &a)
{
    return a.size() > 5;
}

int main(int argc, char* argv[])
{
    vector<string> vec;
    string strtemp;
    cout << "输入vec中内容" << endl;
    while (cin >> strtemp)
        vec.push_back(strtemp);

    cout << "初始内容为:" << endl;
    for (const auto &c : vec)
        cout << c << ends;
    cout << endl;

    auto endoffive = partition(vec.begin(), vec.end(), morethanfive);
    vec.erase(endoffive, vec.end());
    cout << "排序结束后" << endl;
    for (const auto &c : vec)
        cout << c << ends;
    cout << endl;
    return 0;
}

10.14-10.19略
10.20

int main(int argc, char* argv[])
{
    vector<string> vectemp;
    string strtemp;
    while (cin >> strtemp)
        vectemp.push_back(strtemp);
    cout << "尺寸大于6的有" << count_if(vectemp.cbegin(), vectemp.cend(), [](const string &a) {return a.size() > 6; }) << "个" << endl;
    return 0;
}

10.21

    auto f = [c]()mutable->bool {if (c != 0) { cout << c-- << endl; return 0; } else return 1; };

10.22

int main(int argc, char* argv[])
{
    vector<string> vectemp;
    string strtemp;
    while (cin >> strtemp)
        vectemp.push_back(strtemp);
    cout << "尺寸大于6的有" << count_if(vectemp.cbegin(), vectemp.cend(),bind(check_size,_1,6) ) << "个" << endl;
    return 0;
}

10.23
一个可调用对象和它的所有参数
10.24

int main(int argc, char* argv[])
{
    vector<int> vectemp;
    string str;
    cout << "输入一个string" << endl;
    cin >> str;
    int inttemp;
    cout << "输入一些int" << endl;
    while (cin >> inttemp)
        vectemp.push_back(inttemp);
    cout << *find_if(vectemp.cbegin(), vectemp.cend(), bind(check_size, _1, str.size())) << endl;
    return 0;
}

10.25-10.26 略
10.27

int main(int argc, char* argv[])
{
    vector<int> vectemp;
    list<int> listtemp;
    int inttemp;
    cout << "输入一些int值" << endl;
    while (cin >> inttemp)
        vectemp.push_back(inttemp);
    sort(vectemp.begin(),vectemp.end());
    unique_copy(vectemp.begin(), vectemp.end(), back_inserter(listtemp));
    for (const auto c : listtemp)
        cout << c << ends;
    cout << endl;
    return 0;
}

10.28

int main(int argc, char* argv[])
{
    vector<int> vectemp{1,2,3,4,5,6,7,8,9};
    list<int> listtemp;
    unique_copy(vectemp.begin(), vectemp.end(), back_inserter(listtemp));
    for (const auto c : listtemp)
        cout << c << ends;
    cout << endl;

    listtemp.clear();
    unique_copy(vectemp.begin(), vectemp.end(), front_inserter(listtemp));
    for (const auto c : listtemp)
        cout << c << ends;
    cout << endl;

    listtemp.clear();
    unique_copy(vectemp.begin(), vectemp.end(), inserter(listtemp,listtemp.begin()));
    for (const auto c : listtemp)
        cout << c << ends;
    cout << endl;
    return 0;
}

10.29

int main(int argc, char* argv[])
{
    ifstream wenjian("C:/Users/Administrator/Desktop/stupid.txt");
    istream_iterator<string> iter_is(wenjian);
    istream_iterator<string> iter_eof;
    vector<string> vectemp(iter_is,iter_eof);
    for (const auto &c : vectemp)
        cout << c << endl;

    return 0;
}

10.30-10.31

int main(int argc, char* argv[])
{
    istream_iterator<int> iter_is(cin);
    istream_iterator<int> iter_eof;
    vector<int> vectemp(iter_is,iter_eof),vectemp0;
    sort(vectemp.begin(), vectemp.end());
    unique_copy(vectemp.begin(), vectemp.end(), back_inserter(vectemp0));
    for (const auto &c : vectemp0)
        cout << c << ends;
    cout << endl;
    return 0;
}

10.32 略
10.33

int main(int argc, char* argv[])
{
    ifstream wenjian0("C:/Users/Administrator/Desktop/stupid0.txt");
    ofstream wenjian1("C:/Users/Administrator/Desktop/stupid1.txt"), wenjian2("C:/Users/Administrator/Desktop/stupid2.txt");
    ostream_iterator<int> shuchu1(wenjian1, " "), shuchu2(wenjian2, "\r\n");
    istream_iterator<int> iter_is(wenjian0);
    istream_iterator<int> iter_eof;
    vector<int> vectemp(iter_is,iter_eof);
    for (const auto c : vectemp)
    {
        if (c % 2)
            shuchu1 = c;
        else
            shuchu2 = c;
    }
    cout << "整完了" << endl;
    return 0;
}

10.34-10.42 略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值