C++ Primer 答案 第十章

10.1 and 10.2

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;

int main(int argc, char *argv[])
{
    vector<int> vec = {1,1,1,3,3,3,3,3,3,4,5,6,7,7};
    cout << count(vec.cbegin(), vec.cend(), 3) << endl;
    list<string> vec1 = {"a", "a", "a", "dd", "d", "xx"};
    cout << count(vec1.cbegin(), vec1.cend(), "a") << endl;
    return 0;
}

10.3
//以后特别短的简单的就不写全了
cout << accumulate(vec.cbegin(), vec.cend(), 0) << endl;

10.4
会把 double 当 int 来算

10.5
容器里存的是c风格字符串的首地址(char*),它们不能用操作符比较
比较c风格字符串要用 strcmp

10.6

fill_n(c.begin, c.size(), 0);

10.7
(a)
copy函数中 copy的目的序列大小 >= 输入序列的大小(前两个迭代器框定的范围)
因此 加一句: vec.resize(lst.size());
    或者: copy(lst.cbegin(), lst.cend(), back_inserter(vec));
(b)  
vec.reverse(10);
这句话只是分配了空间,vec的size仍然是0, 修改方法和(a)一样

10.8
标准库算法只是把值传给迭代器,而改变容器的操作是由迭代器来完成的

10.9

void elimDups(vector<string> &vec)
{
    sort(vec.begin(), vec.end());
    auto end_unique = unique(vec.begin(), vec.end());
    vec.erase(end_unique, vec.end());
    for (auto i : vec)
        cout << i << " ";
    cout << endl;
}

10.11
我照这教材来的,有漏洞,因为通过stable_sort后相同的单词并没有排在一起,这种情况下执行unique函数就无法达到目的。改造办法是补充 isShort函数,在size相同情况下继续比较字母排序

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

inline bool isShorter(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}
void elimDups(vector<string> &vec)
{
    stable_sort(vec.begin(), vec.end(), isShorter);
    auto end_unique = unique(vec.begin(), vec.end());
    vec.erase(end_unique, vec.end());
    for (auto &i : vec)
        cout << i << " ";
    cout << endl;
}
int main(int argc, char *argv[]) 
{
    vector<string> vec = {"dog", "cat", "orange", "apple", "fish", "bird", "cat", "dog"};
    elimDups(vec);
    return 0;
}

10.12
compareIsbn函数:

bool compareIsbn(const Sales_data& sd1, const Sales_data& sd2)
{
    return sd1.isbn() < sd2.isbn();
}


sort函数:
sort(v.begin(), v.end(), compareIsdn);

10.13

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
using namespace std;

inline bool cmp(const string &s)
{
    return s.size() >= 5;
}
bool fun(vector<string> &v)
{
    auto part = partition(v.begin(), v.end(), cmp);
    if(*part == *v.begin())
        return false;
    for (auto it = v.begin(); it != part; it++)
        cout << *it <<endl;
    return true;
}
int main(int argc, char *argv[]) 
{
    vector<string> vec = {"dog", "cat", "orange", "apple", "fish", "bird", "cat", "dog"};
    fun(vec);  //可以设定个值接受它,表示vec中是否有大于等于5个字符的元素
    return 0;
}

10.14

[ ](const int &x, const int &y){return x + y;}

10.15

[x](int y){return x + y;}

10.17

sort(v.begin(), v.end(), [](const Sales_data& sd1, const Sales_data& sd2){return sd1.isbn() < sd2.isbn();});

10.20

count_if(v.cbegin(), v.cend(), [](const string &s){return s.size() > 6;});

10.21

auto mylambda = [&i](){
    if(i == 0)
        return true;
    else
        i--;
    return false;
};

10.22
头文件可真多

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool cmp(const string &s, size_t size)
{
    return s.size() <= size;
}

int main(int argc, char *argv[])
{
    vector<string> vec = {"dog", "cattttt", "orange", "apple", "fishhh", "bird", "cat", "dog"};
    cout << count_if(vec.cbegin(), vec.cend(), bind(cmp, _1, 5)) <<endl;
    return 0;
}

10.23
1 + 第一个参量(函数)的参量个数

10.24

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const int &i, size_t size)
{
    return i > size;
}

int main(int argc, char *argv[])
{
    string s = "12345";
    vector<int> vec = {2, 4, 5, 7, 3, 4, 6};
    cout << *find_if(vec.cbegin(), vec.cend(), bind(check_size, _1, s.size())) <<endl;
    return 0;
}
//输出 :7

10.26
back_inserter: 相当于push_back
front_inserter: 相当于push_font
inserter: 多了一个参数用于指定插入位置,注意插入元素后该迭代器指向插入位置的后一位
          而insert(ite, value)的返回值是指向执行插入位置,这是它俩的不同点

10.27

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
using namespace std;

int main(int argc, char *argv[])
{
    vector<int> vec = {2, 3, 5, 7, 3, 4, 6, 7, 1};
    list<int> lst;
    sort(vec.begin(), vec.end());
    unique_copy(vec.cbegin(), vec.cend(), inserter(lst, lst.begin()));
    for (auto i : lst)
        cout << i << " ";
    cout << endl;
    return 0;
}
//输出: 1 2 3 4 5 6 7

10.28

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
using namespace std;

int main(int argc, char *argv[])
{
    const vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    list<int> lst1;
    list<int> lst2;
    list<int> lst3;
    copy(vec.cbegin(), vec.cend(), inserter(lst1, lst1.begin()));
    copy(vec.cbegin(), vec.cend(), back_inserter(lst2));
    copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
    for (auto i : lst1)
        cout << i << " ";
    cout << endl;
    for (auto i : lst2)
        cout << i << " ";
    cout << endl;
    for (auto i : lst3)
        cout << i << " ";
    cout << endl;
    return 0;
}
//输出:
//1 2 3 4 5 6 7 8 9
//1 2 3 4 5 6 7 8 9 
//9 8 7 6 5 4 3 2 1

10.34 and 10.35

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

int main(int argc, char *argv[])
{
    const vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    //10.34
    for(auto rit = vec.crbegin(); rit != vec.crend(); rit++)
        cout << *rit << " ";
    cout << endl;
    //10.35
    for(auto rit = vec.cend(); rit != vec.cbegin(); )
        cout << *--rit << " "; //注意哦, *cend()没值,需先--
    cout << endl;
    return 0;
}

10.36
find(lst.crbegin(), lst.crend(), 0);

10.37

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
using namespace std;

int main(int argc, char *argv[])
{
    vector<int> vec ={0,1,2,3,4,5,6,7,8,9};
    list<int> lst;
    copy(vec.crbegin() + 2, vec.crbegin() + 7, back_inserter(lst));
    for(auto i : lst)
        cout << i << " ";
    cout << endl;
    return 0;
}
//输出:
//7 6 5 4 3

10.38
Input iterators : ==, !=, ++, *, ->
Output iterators : ++, *
Forward iterators : ==, !=, ++, *, ->
Bidirectional iterators : ==, !=, ++, --, *, ->
Random-access iterators : ==, !=, <, <=, >, >=, ++, --, +, +=, -, -=, -(two iterators), *, ->, iter[n] == * (iter + n)

10.39
list 属于双向迭代器 (所以啊咱不能 itr += 2; 只能advance(itr, 2);)
vecor 属于随机访问迭代器 (咱还可以下标运算)

10.40
copy : 第一个和第二个参数是输入迭代器, 第三个参数是输出迭代器
reverse : 双向迭代器
unique : 前向迭代器

10.41
(a)在范围内,用new_val替代old_val
(b)在范围内,用new_val替代使pred为真多元素
(c)就是(a)的复制版本,不改变原容器,把改变的复制到新容器
(d)就是(b)的复制版本

10.42

#include <iostream>
#include <list>
using namespace std;

int main(int argc, char *argv[])
{
    list<string> lst = {"cat", "dog", "apple", "dog", "bird", "pig", "cat"};
    lst.sort();
    lst.unique();
    for(auto &i : lst)
        cout << i << " ";
    cout << endl;
    return 0;
}
//输出:
//apple bird cat dog pig

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值