C++Primer第五版 习题答案 第十章 泛型算法

入坑C++、感觉像加入神 教一样魔教教主张无忌

10.1

#include
#include
#include

using namespace std;

int main()
{
	vector<int>v1{ 1,2,4,5,3,1,5,6,8,7,1,1,1,1 };
	cout << count(v1.cbegin(), v1.cend(), 1) << endl;
	return 0;
}

10.2

#include
#include
#include
#include

using namespace std;

int main()
{
	list<string>l1{ "we","he","we","she","it","we" };
	cout << count(l1.cbegin(), l1.cend(), "we") << endl;
	return 0;
}

10.3

#include
#include
#include

using namespace std;

int main()
{
	vector<int>v1{ 1,2,3,4,5,6,7,8,9,10 };
	cout << accumulate(v1.cbegin(), v1.cend(), 0);
	return 0;
}

10.4

第三个参数决定返回值类型,为int,精度降低;

#include
#include
#include

using namespace std;

int main()
{
	vector<double>v1{ 1,2,3,4,5,6,7,8,9,10 };
	cout << accumulate(v1.cbegin(), v1.cend(), 0);
	return 0;
}

10.5

equal会表示指针地址,而不是字符串值,比较结果与string类型不一致;

10.6

#include
#include
#include

using namespace std;

int main()
{
	vector<int>v1{ 1,2,3,4,5,6,7,8,9,10 };
	fill_n(v1.begin(), v1.size(), 0);
	for (const auto i : v1)
		cout << i << " ";
	cout << endl;

	vector<int>v2;
	fill_n(back_inserter(v2), 10, 1);
	for (const auto i : v2)
		cout << i << " ";
	cout << endl;
	return 0;

}

10.7

a)

copy(lst.cbegin(),lst.cend(),back_inserter(vec));

b)
没有报错,但与预期不符合,vec并没有10个元素,且每个元素都为0;

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

10.8

back_inserter是迭代器的一部分,而不是算法的一部分;

10.9

#include
#include
#include

using namespace std;

void elimDups(vector<string>& words)
{
	sort(words.begin(), words.end());
	for (const auto s : words)
		cout << s << " ";
	cout << endl;

	auto unque_end = unique(words.begin(), words.end());
	for ( const auto s : words)
		cout << s << " ";
	cout << endl;
	words.erase(unque_end, words.end());
	for (const auto s : words)
		cout << s << " ";
	cout << endl;
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	elimDups(s1);
}

10.10

算法不执行容器操作,因此不改变容器大小,不改变容器大小使得算法更加通用;

10.11

#include
#include
#include

using namespace std;

vector<string> &elimDups(vector<string>& words)
{
	sort(words.begin(), words.end());
	auto unque_end = unique(words.begin(), words.end());
	words.erase(unque_end, words.end());
	return words;
}
bool isShorter(const string& s1, const string& s2)
{
	return s1.size() < s2.size();
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	for (const auto s : elimDups(s1))
		cout << s << " ";
	cout << endl;
	stable_sort(s1.begin(), s1.end(), isShorter);
	for (const auto s : s1)
		cout << s << " ";
	cout << endl;
	return 0;

}

10.12

#include 
#include 
#include 
#include 
#include "../ch07_Classes/Sales_data_ex26.h"
#include 
#include 
#include 

using namespace std;

bool compareIsbn(const Sales_data &sales_data1, const Sales_data &sales_data2)
{
	return sales_data1.isbn() < sales_data2.isbn();
}

int main()
{
	vector<string> v1;

	ifstream is("../ch08_The_IO_Library/book_sales");
	string buf;

	if(!is)
	{
		cerr << "open error" << endl;
		return -1;
	}

	while(getline(is, buf))
		v1.push_back(buf);

	vector<Sales_data> v2;
	for(const auto &s : v1)
	{
		// cout << s << endl;
		istringstream iss(s);
		v2.push_back(Sales_data(iss));
	}

	stable_sort(v2.begin(), v2.end(), compareIsbn);

	for(const auto s : v2)
		cout << s.isbn() << endl;

	return 0;
}

10.13

#include
#include
#include

using namespace std;

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

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	for (const auto s : s1)
		cout << s << " ";
	cout << endl;
	auto iter = partition(s1.begin(), s1.end(), greater_than_5);
	s1.erase(iter, s1.end());

	for (const auto s : s1)
		cout << s << " ";
	cout << endl;
	return 0;
}

10.14

#include

using namespace std;

int main()
{
	auto add_int = [](int x, int y) {return x + y; };
	cout << add_int(1, 2)<<endl;;
	return 0;
}

10.15

#include

using namespace std;

int main()
{
	int x = 1;
	auto add_int = [x](int y) {return x + y; };
	cout << add_int(2)<<endl;;
	return 0;
}

10.16

#include
#include
#include
#include

using namespace std;

vector<string>& elimdUps(vector<string >& words)
{
	sort(words.begin(), words.end());
	auto unique_end = unique(words.begin(), words.end());
	words.erase(unique_end, words.end());
	return words;
}

void bidggies(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 << endl;
	for_each(wc, words.end(), [](const string& s) {cout << s << " "; });
	cout << endl;
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	bidggies(s1, 5);
	return 0;
}

10.17

#include 
#include 
#include 
#include 
#include "../ch07_Classes/Sales_data_ex26.h"
#include 
#include 
#include 

using namespace std;

int main()
{
	vector<string> v1;

	ifstream is("../ch08_The_IO_Library/book_sales");
	string buf;

	if(!is)
	{
		cerr << "open error" << endl;
		return -1;
	}

	while(getline(is, buf))
		v1.push_back(buf);

	vector<Sales_data> v2;
	for(const auto &s : v1)
	{
		// cout << s << endl;
		istringstream iss(s);
		v2.push_back(Sales_data(iss));
	}

	stable_sort(v2.begin(), v2.end(), []
		(const Sales_data &sales_data1, const Sales_data &sales_data2)
			{ return sales_data1.isbn() < sales_data2.isbn(); });

	for(const auto s : v2)
		cout << s.isbn() << endl;

	return 0;
}

10.18

#include
#include
#include
#include

using namespace std;

vector<string>& elimdUps(vector<string >& words)
{
	sort(words.begin(), words.end());
	auto unique_end = unique(words.begin(), words.end());
	words.erase(unique_end, words.end());
	return words;
}

void bidggies(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 = wc-words.begin();
	cout << count << endl;
	for_each(words.begin(),wc, [](const string& s) {cout << s << " "; });
	cout << endl;
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	bidggies(s1, 5);
	return 0;
}

10.19

#include
#include
#include
#include

using namespace std;

vector<string>& elimdUps(vector<string >& words)
{
	sort(words.begin(), words.end());
	auto unique_end = unique(words.begin(), words.end());
	words.erase(unique_end, words.end());
	return words;
}

void bidggies(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 = wc-words.begin();
	cout << count << endl;
	for_each(words.begin(),wc, [](const string& s) {cout << s << " "; });
	cout << endl;
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	bidggies(s1, 5);
	return 0;
}

10.20

#include
#include
#include
#include

using namespace std;

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	string::size_type sz = 6;
	cout << count_if(s1.begin(), s1.end(), [sz](const string& s) {return s.size() >= sz; });
	return 0;
}

10.21

#include
#include
#include
#include

using namespace std;

int main()
{
	int i = 5;
	auto reduce_i = [&i]()->bool 
	{
		if (i <= 0)
			return false;
		else
		{
			--i;
			return true;
		}
	};

	while (reduce_i())
	{
		cout << i << endl;
	}
	return 0;
}

10.22

#include
#include
#include
#include
#include

using namespace std;

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

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	cout << count_if(s1.begin(), s1.end(), bind(judue_size, placeholders::_1, 6)) << endl;
}

10.23

n+1。假设函数有n个参数,则绑定这n个参数加上函数本身;

10.24

#include
#include
#include
#include
#include

using namespace std;
bool cheak_size(string& s, int sz)
{
	return s.size() < sz;
}

int main()
{
	vector<int >v1{ 1,2,3,4,5,6 };
	string s1{ "he" };
	auto iter = find_if(v1.begin(), v1.end(), bind(cheak_size, s1, placeholders::_1));
	cout << *iter << endl;
	return 0;
}

10.25

#include
#include
#include
#include
#include

using namespace std;

vector<string>& elimdUps(vector<string >& words)
{
	sort(words.begin(), words.end());
	auto unique_end = unique(words.begin(), words.end());
	words.erase(unique_end, words.end());
	return words;
}

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

void bidggies(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(), bind(cheak_size,placeholders::_1,sz));
	auto count = wc - words.begin();
	cout << count << endl;
	for_each(words.begin(), wc, [](const string& s) {cout << s << " "; });
	cout << endl;
}

int main()
{
	vector<string> s1{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	bidggies(s1, 5);
	return 0;
}

10.26

back_inserter创建一个push_back 的迭代器;
front_inserter创建一个push_front的迭代器;
inserter创建一个inserter迭代器。此函数接受第二个参数,这个参数必须是指向给定容器的迭代器,元素将被插入到给定迭代器所表示的元素之前;

10.27

#include
#include
#include
#include

using namespace std;
 
int main()
{
	list<int>l1;
	vector<int> v1{ 1,5,2,1,4,6,3,2,6,5,7,8,9,4,9,1,2 };
	sort(v1.begin(), v1.end());
	unique_copy(v1.begin(), v1.end(), back_inserter(l1));
	for (const auto i : l1)
		cout << i << " ";
	cout << endl;
	return 0;
}

10.28

#include
#include
#include
#include

using namespace std;
 
int main()
{
	list<int>l1,l2,l3;
	vector<int> v1{ 1,2,3,4,5,6,7,8,9 };
	copy(v1.begin(), v1.end(), inserter(l1,l1.begin()));
	for (const auto i : l1)
		cout << i << " ";
	cout << endl;

	copy(v1.begin(), v1.end(), back_inserter(l2));
	for (const auto i : l2)
		cout << i << " ";
	cout << endl;

	copy(v1.begin(), v1.end(), front_inserter(l3));
	for (const auto i : l3)
		cout << i << " ";
	cout << endl;
}

10.29

#include
#include
#include
#include

using namespace std;
 
int main()
{
	ifstream ifs("letter.txt");
	istream_iterator<string> str_istream_iter(ifs), eof;
	vector<string> v1(str_istream_iter, eof);
	ostream_iterator<string> str_ostream_iter(cout, " ");
	copy(v1.begin(), v1.end(), str_ostream_iter);
	return 0;
}
 	

10.30

#include
#include
#include

using namespace std;
 
int main()
{
	istream_iterator<int> int_cin(cin), eof;
	vector<int>v1(int_cin, eof);
	sort(v1.begin(), v1.end());
	ostream_iterator<int> out_iter(cout, " ");
	copy(v1.begin(), v1.end(),out_iter);
	return 0;
}

10.31

#include
#include
#include

using namespace std;
 
int main()
{
	istream_iterator<int> int_cin(cin), eof;
	vector<int>v1(int_cin, eof);
	sort(v1.begin(), v1.end());
	ostream_iterator<int> out_iter(cout, " ");
	unique_copy(v1.begin(), v1.end(),out_iter);
	return 0;
}

10.32

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

using namespace std;

int main()
{
	istream_iterator<Sales_item> int_cin(cin), eof;
	vector<Sales_item> v1(int_cin, eof);
	sort(v1.begin(), v1.end(), compareIsbn);
	// for(const auto s : v1)
	// 	cout << s.isbn() << endl;
	cout << endl;
	for(auto beg = v1.cbegin(), end = beg; beg != v1.cend(); beg = end)
	{
		end = find_if(beg, v1.cend(), [beg](const Sales_item &sale_item){ return sale_item.isbn() != beg->isbn(); });
		cout << accumulate(beg, end, Sales_item(beg->isbn())) << endl;
	}

	return 0;
}

10.33

#include
#include
#include
#include


using namespace std;
 
int main(int argc,char **argv)
{
	if (argc != 4)return -1;
	ifstream ifs(argv[1]);
	istream_iterator <int> int_istream_iter(ifs), eof;
	vector<int>v1(int_istream_iter, eof);
	
	ofstream ofs_odd("argv[2]");
	ostream_iterator<int> odd_iter(ofs_odd, " ");
	copy_if(v1.begin(), v1.end(),odd_iter, [](int i) {return i % 2; });
	ofs_odd.close();


	ofstream ofs_even("argv[3]");
	ostream_iterator<int> even_iter(ofs_even, " ");
	copy_if(v1.begin(), v1.end(), even_iter, [](int i) {return !(i % 2); });
	ofs_even.close();
	
}

10.34

#include
#include

using namespace std;
 
int main()
{
vector<int>v1{ 1,2,3,4,5,6,7 };
	for (auto r_iter = v1.crbegin(); r_iter != v1.crend(); ++r_iter)
		cout << *r_iter << " ";
	cout << endl;
	return 0;
}

10.35

#include 
#include 
#include 

using namespace std;

int main()
{
vector<int> v1 = { 1,2,3,4,5,6,7,8 };
	for (auto iter = v1.cend() - 1; iter != v1.cbegin() ; --iter)
		cout << *iter << " ";
	cout << *v1.cbegin() << " ";
	cout << endl;
	return 0;
}

10.36

#include 
#include 
#include

using namespace std;

int main()
{
	list<int>l1{ 0,1,2,3,4,5,6,0 };
	auto r_iter = find(l1.crbegin(), l1.crend(), 0);
	cout <<*r_iter << endl;
}

10.37

#include 
#include 
#include
#include

using namespace std;

int main()
{
	vector<int>v1{ 0,1,2,3,4,5,6,7,8,9 };
	list<int>l1(v1.crbegin() + 2, v1.crbegin() + 7);
	for (const auto i : l1)
		cout << i << " ";
	cout << endl;
	return 0;
}

10.38

输入迭代器: == , != , ++,*, ->;
输出迭代器:++, *;
前向迭代器: ,!=,++,*,->;
双向迭代器:
,!=,++,–,*,->;
随机访问迭代器 ==,!=,++,–,*,->,<=,>,>=,+,+=,-;

10.39

list是双向迭代器;
vector属于随机访问迭代器;

10.40

copy 前两个输入迭代器,后一个输出迭代器;
reverse 双向迭代器;
unique 前向迭代器;

10.41

replace(beg, end, old_val, new_val); // 在beg与end之间将old_val替换为new_val;
replace_if(beg, end, pred, new_val); //在beg和end之间,如果谓词条件满足则替换为new_val;
replace_copy(beg, end, dest, old_val, new_val); //在beg和end之间,如果是old_val替换为new_val,不改变原始值,将结果复制到dest;
replace_copy_if(beg, end, dest, pred, new_val);//在beg和end之间,如果满足谓词则替换为new_val,将结果复制到dest;

10.42

#include 
#include
#include
#include

using namespace std;

list<string>& elimDups(list<string >& s)
{
	s.sort();
	s.unique();
	return s;
}
int main()
{
	list<string>l1{ "a","b","a","e","c","g","d" };
	for (const auto s : elimDups(l1))
		cout << s << " ";
	cout << endl;
	return 0;
}

你可能感兴趣的:(C++,c++,算法,数据结构)