Chapter 10: Generic Algorithms

Exercises Section 10.1

Ex10.1

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    int value;
    int number = 15;
    vector<int> vec;
    while (cin >> value)
        vec.push_back(value);
    auto cnt = count(vec.cbegin(), vec.cend(), number);
    cout << cnt << endl;
    system("pause");
    return 0;
}

Ex10.2

#include<iostream>
#include<algorithm>
#include<list>
#include<string>

using namespace std;

int main()
{
    string word = "astralcon";
    string value;
    list<string> lst;
    while (cin >> value)
        lst.push_back(value);
    auto cnt = count(lst.cbegin(), lst.cend(), word);
    cout << cnt << endl;
    system("pause");
    return 0;
}

Exercises Section 10.2.1

Ex10.3

#include<iostream>
#include<numeric>
#include<vector>

using namespace std;

int main()
{
    int value;
    vector<int> vec;
    while (cin >> value)
        vec.push_back(value);
    int sum = accumulate(vec.cbegin(), vec.cend(), 0);
    cout << sum << endl;
    system("pause");
    return 0;
}

Ex10.4

accumulate函数的第三个参数决定了该函数的返回类型,由于 v 为 vector<double>,故第三个参数为 int 类型的值 0 应该改为 double 类型的值 0.0

Ex10.5

如果所有的 rosters 都变为 char *类型,那么 equal函数就是比较两个指针的地址是否相同,而不是比较两个指针指向的对象内容是否相等

Exercises Section 10.2.2

Ex10.6

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    vector<double> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    fill_n(vec.begin(), 10, 0);
    for (double d : vec)
        cout << d << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.7

a)  // 错误;必须保证目的容器的元素数量范围至少与输入容器元素数量范围相等
	vector<int> vec;
    list<int> lst;
    int i;
    while (cin >> i)
        lst.push_back(i);
    copy(lst.cbegin(), lst.cend(), vec.begin());

b)  // 错误;reserve 只是给容器预分配了容纳10个 int 元素的空间,容器内元素数量还是为0,而 fill_n 作用于空容器的结果是未定义的
	vector<int> vec;
    vec.reserve(10);    
    fill_n(vec.begin(), 10, 0);

Ex10.8

back_inserter 定义在 iterator 头文件中而不是 algorithm 头文件中;
algorithm 中的方法不能改变容器元素数量,但是迭代器可以。

Exercises Section 10.2.3

Ex10.9

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

void elimDups(vector<string> &words)
{
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
}

int main()
{
    vector<string> vec;
    string word;
    while (cin >> word)
        vec.push_back(word);
    for (const string &s : vec)
        cout << s << " ";
    cout << endl;
    cout << "===after call elimDups===" << endl;
    elimDups(vec);
    for (const string &s: vec)
        cout << s << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.10

泛型算法可以用于不同类型的容器,通过迭代器对容器进行访问,即使改变了容器的元素数量大小,也是迭代器的作用而不是泛型算法。

Exercises Section 10.3.1

Ex10.11

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

void elimDups(vector<string> &words)
{
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
}

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

int main()
{
    vector<string> words;
    string word;
    while (cin >> word)
        words.push_back(word);
    for (const string &s : words)
        cout << s << " ";
    cout << endl;
    elimDups(words);
    stable_sort(words.begin(), words.end(), isShorter);
    cout << "================" << endl;
    for (const string &s : words)
        cout << s << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.12

#include<iostream>
#include<algorithm>
#include<vector>
#include "Sales_item.h"

using namespace std;



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

int main()
{
    vector<Sales_data> vec;
    Sales_data sd;
    while (read(cin, sd))
        vec.push_back(sd);
    sort(vec.begin(), vec.end(), compareIsbn);
    for (const auto &s : vec)
        cout << s << endl;
    system("pause");
    return 0;
}

Ex10.13

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

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

int main()
{
    vector<string> words;
    string word;
    while (cin >> word)
        words.push_back(word);
    auto pos = partition(words.begin(), words.end(), isLonger);
    for (auto beg = words.begin(); beg != pos; ++beg)
        cout << *beg << " ";
    cout << endl;
    system("pause");
    return 0;
}

Exercises Section 10.3.2

Ex10.14

#include<iostream>

using namespace std;

int main()
{
    int a = 1, b = 2;
    auto f = [](int a,int b) { return a + b; };
    cout << f(a, b) << endl;
    system("pause");
    return 0;
}

Ex10.15

#include<iostream>

using namespace std;

int main()
{
    int a = 1, b = 2;
    auto f = [a](int b) { return a + b; };
    cout << f(b) << endl;
    system("pause");
    return 0;
}

Ex10.16

void biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(words);
    stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size();});
    auto wc = find_if(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz;});
    auto count = words.end() - wc;
    cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;
    for_each(wc, words.end(), [](const string &s) {cout << s << " ";});
    cout << endl;
}

Ex10.17

#include<iostream>
#include<algorithm>
#include<vector>
#include "Sales_item.h"

using namespace std;

int main()
{
    vector<Sales_data> vec;
    Sales_data sd;
    while (read(cin, sd))
        vec.push_back(sd);
    sort(vec.begin(), vec.end(), [](const Sales_data &s1, const Sales_data &s2) {return s1.isbn() < s2.isbn();});
    for (const auto &s : vec)
        cout << s << endl;
    system("pause");
    return 0;
}

Ex10.18

void biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(words);
    stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size();});
    auto wc = partition(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz;});
    auto count = words.end() - wc;
    cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;
    for_each(wc, words.end(), [](const string &s) {cout << s << " ";});
    cout << endl;
}

Ex10.19

void biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(words);
    stable_sort(words.begin(), words.end(), [](const string &a, const string &b) {return a.size() < b.size();});
    auto wc = stable_partition(words.begin(), words.end(), [sz](const string &a) {return a.size() >= sz;});
    auto count = words.end() - wc;
    cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;
    for_each(wc, words.end(), [](const string &s) {cout << s << " ";});
    cout << endl;
}

Exercises Section 10.3.3

Ex10.20

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

int main()
{
    vector<string> vec;
    string word;
    while (cin >> word)
        vec.push_back(word);
    auto f = count_if(vec.begin(), vec.end(), [](const string &s) {return s.size() > 6;});
    cout << f << endl;
    system("pause");
    return 0;
}

Ex10.21

#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    int value;
    cin >> value;
    auto f = [& value]() mutable -> bool {if(value != 0) {return false; --value;} else return true;};
    cout << f() << endl;
    system("pause");
    return 0;
}

Exercises Section 10.3.4

Ex10.22

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

void elimdups(vector<string> &words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}

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

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

ostream & print(ostream &os, const string &s, char c)
{
	return os << s << c;
}

void biggies(vector<string> &words, vector<string>::size_type sz)
{
	elimdups(words); 
	stable_sort(words.begin(), words.end(), bind(ishorter,_1,_2));
	auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, sz));
	auto count = words.end() - wc;
	for_each(wc, words.end(), bind(print,ref(cout), _1, ' '));
}

int main(void)
{
	vector<string> words;
	string word;
	while (cin >> word)
	{
		words.push_back(word);
	}
	biggies(words, 5);
	
	return 0;
}

Ex10.23

two arguments

Ex10.24

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;
using namespace placeholders;

bool check_size(const string &s1, string::size_type sz)
{
	return s1.size() < sz;
}
int main(void)
{
	string s1("hello");
	vector<int> vec1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
	auto wc = find_if(vec1.begin(), vec1.end(), bind(check_size, s1,_1));
	int count = vec1.end() - wc;
	cout << "vec1中第" <<vec1.size()-count << "个第一个大于string长度" << endl;
    system("pause");
    return 0;
}

Ex10.25

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

void elimdups(vector<string> &words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}

ostream &print(ostream &os,const string &s, const char c)
{
	return os << s << c;
}

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

void biggies(vector<string> & words, vector<string>::size_type sz)
{
	elimdups(words); 
	for_each(words.begin(), words.end(), bind(print,ref(cout),_1,' '));
	cout << endl;
	auto wc = partition(words.begin(), words.end(), bind(check_size,_1,sz));
	for_each(words.begin(),wc, bind(print, ref(cout), _1, ' '));
	cout << endl;
}

int main(void)
{
	vector<string> words{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	biggies(words, 4);
	return 0;
}

Exercises Section 10.4.1

Ex10.26

back_inserter:创建一个使用 push_back的迭代器
front_inserter:创建一个使用push_front的迭代器
inserter:创建一个使用insert的迭代器
front_inserter产生一个迭代器使插入的顺序相反;而back_inserter和inserter则不会。

Ex10.27

#include<iostream>
#include<algorithm>
#include<vector>
#include<list>

using namespace std;

int main()
{
    vector<int> vec = {1, 1, 2, 3, 4, 5};
    list<int> lst;
    lst.resize(vec.size());
    unique_copy(vec.begin(), vec.end(), lst.begin());
    for (const auto &i : lst)
        cout << i << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.28

#include<iostream>
#include<algorithm>
#include<vector>
#include<list>

using namespace std;

int main()
{
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    vector<int> v1, v3;
    list<int> v2;
    copy(vec.cbegin(), vec.cend(), back_inserter(v1));
    copy(vec.cbegin(), vec.cend(), front_inserter(v2));
    copy(vec.cbegin(), vec.cend(), inserter(v3, v3.begin()));
    for (const auto &i : v1)
        cout << i << " ";
    cout << endl;
    for (const auto &i : v2)
        cout << i << " ";
    cout << endl;
    for (const auto &i : v3)
        cout << i << " ";
    cout << endl;
    system("pause");
    return 0;
}

Exercises Section 10.4.3

Ex10.34

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    vector<int> vec;
    int value;
    while (cin >> value)
        vec.push_back(value);
    for (auto r_iter = vec.crbegin(); r_iter != vec.crend(); ++r_iter)
        cout << *r_iter << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.35

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    vector<int> vec;
    int value;
    while (cin >> value)
        vec.push_back(value);
    vector<int>::iterator it = vec.end();
    do 
    {
        --it;
        cout << *it << " ";
    } while (it != vec.begin());
    cout << endl;
    system("pause");
    return 0;
}

Ex10.36

#include<iostream>
#include<algorithm>
#include<list>

using namespace std;

int main()
{
    list<int> lst;
    int value;
    while (cin >> value)
        lst.push_back(value);
    auto pos = find(lst.crbegin(), lst.crend(), 0);
    cout << "Output elements after the last element in a list with value zero: ";
    for (auto iter = pos.base(); iter != lst.cend(); ++iter)
        cout << *iter << " ";
    cout << endl;
    system("pause");
    return 0;
}

Ex10.37

#include<iostream>
#include<algorithm>
#include<list>
#include<vector>

using namespace std;

int main()
{
    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 (const int &i : lst)
        cout << i << " ";
    cout << endl;
    system("pause");
    return 0;
}

Exercises Section 10.5.1

Ex10.38

input iterator: 只读不写,单遍扫描,只递增;支持 == != 前缀和后缀++ 解引用* 箭头操作 -> 
output iterator: 只写不读,单遍扫描,只递增;支持前缀和后缀++ 解引用*
forward iterator: 可读写,多遍扫描,只递增;支持所有输入输出迭代器操作
bidirectional iterator: 可读写,多遍扫描,可递增递减;支持所有 forward iterator 的操作,加上前置和后置--
random-access iterator: 可读写,多遍扫描,支持上述全部迭代器运算,外加关系运算符、迭代器减法、下标运算符

Ex10.39

list: bidirectional iterator
vector: random-access iterator

Ex10.40

copy:前两个参数至少为输入迭代器,第三个参数至少为输出迭代器
reverse:两个参数至少为 forward iterator

Exercises Section 10.5.3

Ex10.41

replace(beg, end, old_val, new_val);	// 将[beg, end)中的 old_val 替换为 new_val
replace_if(beg, end, pred, new_val);	// 将[beg, end)中满足 pred 的元素值替换为 new_val
replace_copy(beg, end, dest, old_val, new_val);	// 将[beg, end)的元素复制到 dest,并将其值为 old_val 的元素替换为 new_val
replace_copy_if(beg, end, dest, pred, new_val);	// 将[beg, end)的元素复制到 dest,并将满足 pred 的元素值替换为 new_val

Exercises Section 10.6

Ex10.42

#include<iostream>
#include<algorithm>
#include<list>

using namespace std;

void elimDups(list<string> &words)
{
    words.sort();
    words.unique();
}

int main()
{
    list<string> words;
    string  word;
    while (cin >> word)
        words.push_back(word);
    elimDups(words);
    for (const string &s : words)
        cout << s << " ";
    cout << endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值