C++ Primer 第十章答案

习题10.1-10.2
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
using namespace std;

int main(int argc, char** argv)
{
    vector<int> vec;
    list<string> lis;
    int buf_int;
    string buf_str;

    cout << "Please input some value to vec" << endl;
    while (cin >> buf_int ) {
        if (buf_int == -1)
            break;
        vec.push_back(buf_int);
    }

    cout << "Please input some string to lis" << endl;
    while (cin >> buf_str) {
        if (buf_str == "-1")
            break;
        lis.push_back(buf_str);
    }
    cout << "Please input a value which you want find in the vec" << endl;
    int vaule;
    cin >> vaule;
    auto num_value = count(vec.begin(), vec.end(), vaule);
    cout << "The num of value in the vec is " << num_value << endl;

    cout << "Please input a string which you want find in the lis" << endl;
    string str;
    cin >> str;
    auto num_str = count(lis.begin(), lis.end(), str);
    cout << "The num of string in the lis is " << num_str << endl;

    system("pause");
    return 0;
}

习题10.3
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;

int main(int argc, char** argv)
{
    vector<int> vec = {1,2,3,4,5,6,7,8};
    int sum = accumulate(vec.begin(),vec.end(),0);
    cout << sum << endl;
    system("pause");
    return 0;
}

习题10.4
如果第三个形参是int,那么accumulate的结果也是int。

习题10.5
总会返回0,对比C风格字符串,应该使用strcmp

习题10.6
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;

int main(int argc, char** argv)
{
    vector<int> vec = {1,2,3,4,5,6,7,8};

    fill_n(vec.begin(),vec.size(),0);
    for (auto a : vec)
    {
        cout << a << " ";
    }

    system("pause");
    return 0;
}

习题10.7
(a)vec的size为0,无法拷贝
(b)vec使用reserve后,该vec的最大容纳变为10,但size依旧是0.

习题10.8
back_inserter被包含在iterator中,调用插入迭代器,相当于调用一次push_back。
fill_n(vec.begin(),10,0)
fill_n(back_inserter(vec),10,0)
上述区别就在于,第二项是向vec插入十个0。而第一项是将vec中前是个元素置为0.

习题10.9
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


template <typename Sequence> auto println(Sequence const& seq) -> std::ostream &
{
    for (auto const& elem : seq) std::cout << elem << " ";
    return std::cout << std::endl;
}

auto eliminate_duplicates(std::vector<std::string>& vs)
-> std::vector<std::string> &
{
    std::sort(vs.begin(), vs.end());
    println(vs);
    auto new_end = std::unique(vs.begin(), vs.end());
    println(vs);
    vs.erase(new_end, vs.end());
    return vs;
}

int main()
{
    std::vector<std::string> vs{ "a", "v", "a", "s", "v", "a", "a" };
    println(vs);
    println(eliminate_duplicates(vs));

    system("pause");
    return 0;
}

习题10.10
实际上,algorithms操作的是iterator而不是容器本身。仅通过迭代器,无法改变容器的大小。

习题10.11
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
template <typename Sequence> auto println(Sequence const& seq) -> std::ostream &
{
    for (auto const& elem : seq) std::cout << elem << " ";
    return std::cout << std::endl;
}

auto eliminate_duplicates(std::vector<std::string>& vs)
-> std::vector<std::string> &
{
    std::sort(vs.begin(), vs.end());
    auto new_end = std::unique(vs.begin(), vs.end());
    vs.erase(new_end, vs.end());
    return vs;
}

bool isShorter(const string &str1, const string &str2)
{
    return str1.size() < str2.size();
}
int main()
{
    std::vector<std::string> vs{ "aQ", "v12", "a22", "s222", "v22", "a1", "a1" };

    eliminate_duplicates(vs);
    stable_sort(vs.begin(), vs.end(), isShorter);
    println(vs);
    system("pause");
    return 0;
}

习题10.12

习题10.13
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isLonger(const string &str)
{
    return str.size() > 5;
}
int main()
{
    std::vector<std::string> vs{ "aQ1111", "v121111", "a221111", "s222", "v22", "a1", "a1" };

    auto newEnd = partition(vs.begin(), vs.end(), isLonger);

    for (auto it = vs.begin(); it != newEnd; it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值