C++ Primer: Chapter 10 Generic Algorithm

Code

10.2.2 back_inserter

vector<int> vec;
auto it = back_inserter(vec);
*it = 42;
cout << vec.at(0) << endl;

10.3.2 lambda

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

	auto end_unique = unique(words.begin(), words.end());
	for (auto &s : words)
		cout << s << " ";
	cout << endl;
	//cout << words.size() << endl;

	words.erase(end_unique, words.end());
	for (auto &s : words)
		cout << s << " ";
	cout << endl;
	//cout << words.size() << endl;

}

void bigges(vector<string>& words, vector<string>::size_type& sz)
{
	//按字典顺序排序,删除重复单词
	elimDups(words);
	//按string由短到长排序
	stable_sort(words.begin(), words.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
	//找到第一个长度大于等于sz的string
	auto it = find_if(words.begin(), words.end(), [sz](const string& s1) {return s1.size() >= sz; });
	//计算长度大于等于sz的string数目
	auto cnt = words.end() - it;

	cout << cnt << (cnt > 1 ? " words" : " word") << " of length " << sz << " or longer" << endl;
	for_each(it, words.end(), [](const string& s) {cout << s << " "; }); cout << endl;

}

int main() 
{

	vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	vector<string>::size_type bigger_cnt = 3;
	bigges(story, bigger_cnt);

    return 0;
}

10.4.1 inserter

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5, 6,7, 8, 9 };

	auto beg = vec.begin();
	auto it = inserter(vec, beg);

	//使用插入迭代器向beg所指元素的前面插入元素
	it = 0;
	it = 10;


	for (auto& i : vec)
		cout << i << " ";

	return 0;
}

Homework

Test 10.1

vector<int> ivec{ 1, 1, 1, 2, 3, 4, 4, 5, 6 };

cout << count(ivec.begin(), ivec.end(), 1) << endl;

Test 10.3

vector<int> ivec{ 1, 1, 1, 2, 3, 4, 4, 5, 6 };

cout << accumulate(ivec.begin(), ivec.end(), 0) << endl;

Test 10.5

const char s1[] = "aaaa";
const char s2[] = "aaaa";
vector<const char*> svec1{ s1, s1 };
vector<const char*> svec2{ s2, s2 };

cout << (equal(svec1.cbegin(), svec1.cend(), svec2.cbegin()) ? "equal" : "unequal") << endl;

Test 10.6

vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
fill_n(back_inserter(vec), vec.size(), 0);

Test 10.9

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

	auto end_unique = unique(words.begin(), words.end());
	for (auto &s : words)
		cout << s << " ";
	cout << endl;
	cout << words.size() << endl;

	words.erase(end_unique, words.end());
	for (auto &s : words)
		cout << s << " ";
	cout << endl;
	cout << words.size() << endl;

}

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

    return 0;
}

Test 10.11

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

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

    return 0;
}

Test 10.12

bool compareISBN(my_sales_data& data1, my_sales_data& data2)
{
	return data1.isbn() < data2.isbn();
}

int main() 
{

	my_sales_data data1("book1001");
	my_sales_data data2("book1002");
	my_sales_data data3("book1003");
	my_sales_data data4("book1004");
	my_sales_data data5("book1005");

	vector<my_sales_data> vec{ data2, data3, data5, data1, data4 };
	sort(vec.begin(), vec.end(), compareISBN);

	for (auto& s : vec)
		cout << s.isbn() << " ";
	cout << endl;

    return 0;
}

Test 10.13

bool strLength(string& s1)
{
    return s1.size() < 5;
}

int main() 
{
    vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
    partition(story.begin(), story.end(), strLength);
    for (auto& s : story)
        if (s.size() >= 5)
            cout << s << " ";
    cout << endl;

    return 0;
}

Test 10.14

auto f = [](const int& i1, const int& i2) {return i1 + i2; };

Test 10.15

int lambdaadd(const int& i)
{
	auto f = [i](const int& i2) {return i + i2; };
	return f(2);
}

Test 10.16

void bigges(vector<string>& words, vector<string>::size_type& sz)
{
	//按字典顺序排序,删除重复单词
	elimDups(words);
	//按string由短到长排序
	stable_sort(words.begin(), words.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
	//找到第一个长度大于等于sz的string
	auto it = find_if(words.begin(), words.end(), [sz](const string& s1) {return s1.size() >= sz; });
	//计算长度大于等于sz的string数目
	auto cnt = words.end() - it;

	cout << cnt << (cnt > 1 ? " words" : " word") << " of length " << sz << " or longer" << endl;
	for_each(it, words.end(), [](const string& s) {cout << s << " "; }); cout << endl;

}

int main() 
{

	vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	vector<string>::size_type bigger_cnt = 3;
	bigges(story, bigger_cnt);

    return 0;
}

Test 10.17

int main() 
{
	my_sales_data data1("book1001");
	my_sales_data data2("book1002");
	my_sales_data data3("book1003");
	my_sales_data data4("book1004");
	my_sales_data data5("book1005");

	vector<my_sales_data> vec{ data2, data3, data5, data1, data4 };
	sort(vec.begin(), vec.end(), [](const my_sales_data & data1, const my_sales_data & data2) { return data1.isbn() < data2.isbn(); });

	for (auto& s : vec)
		cout << s.isbn() << " ";
	cout << endl;

    return 0;
}

Test 10.18


void bigges(vector<string>& words, vector<string>::size_type& sz)
{
	//按字典顺序排序,删除重复单词
	elimDups(words);
	//按string由短到长排序
	stable_sort(words.begin(), words.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
	//找到第一个长度大于等于sz的string
	auto it = partition(words.begin(), words.end(), [sz](const string& s1) {return s1.size() >= sz; });
	//计算长度大于等于sz的string数目
	auto cnt = it - words.begin();

	cout << cnt << (cnt > 1 ? " words" : " word") << " of length " << sz << " or longer" << endl;
	for_each(it, words.end(), [](const string& s) {cout << s << " "; }); cout << endl;

}

int main() 
{
	vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	vector<string>::size_type bigger_cnt = 3;
	bigges(story, bigger_cnt);

    return 0;
}

Test 10.19

void bigges(vector<string>& words, vector<string>::size_type& sz)
{
	//按字典顺序排序,删除重复单词
	elimDups(words);
	//按string由短到长排序
	stable_sort(words.begin(), words.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
	//找到第一个长度大于等于sz的string
	auto it = stable_partition(words.begin(), words.end(), [sz](const string& s1) {return s1.size() >= sz; });
	//计算长度大于等于sz的string数目
	auto cnt = it - words.begin();

	cout << cnt << (cnt > 1 ? " words" : " word") << " of length " << sz << " or longer" << endl;
	for_each(it, words.end(), [](const string& s) {cout << s << " "; }); cout << endl;

}

Test 10.20

vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtlee" };
cout << count_if(story.begin(), story.end(), [](const string& s) {return s.size() > 6; });

Test 10.21

void subtract()
{
	int i = 5;
	auto f = [&i]() mutable -> bool { if (i == 0) return true; else { --i; return false; } };
	while (i != 0)
	{
		cout << i << " ";
		f();
	}
	
	cout << i << endl;
}

Test 10.22

#include<functional>
using namespace std::placeholders;

bool filiter6(const string& s, string::size_type sz)
{
	return s.size() < 6;
}

int main() 
{
	vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	cout << count_if(story.begin(), story.end(), bind(filiter6, _1, 6));
    return 0;
}

Test 10.24

bool check_size(const int& i, string::size_type sz)
{
	return i >= sz;
}

int main() 
{
	string str("hello");
	vector<int> ivec{ 3, 4, 2, 6, 5, 4 };
	cout << *find_if(ivec.begin(), ivec.end(), bind(check_size, _1, str.size()));

    return 0;
}

Test 10.25

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

void bigges(vector<string>& words, vector<string>::size_type& sz)
{
	//按字典顺序排序,删除重复单词
	elimDups(words);
	//按string由短到长排序
	stable_sort(words.begin(), words.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
	//找到第一个长度大于等于sz的string
	auto it = partition(words.begin(), words.end(), bind(check_size, _1, sz) );
	//计算长度大于等于sz的string数目
	auto cnt = it - words.begin();

	cout << cnt << (cnt > 1 ? " words" : " word") << " of length " << sz << " or longer" << endl;
	for_each(it, words.end(), [](const string& s) {cout << s << " "; }); cout << endl;

}

int main()
{
	vector<string> story{ "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	vector<string>::size_type bigger_cnt = 3;
	bigges(story, bigger_cnt);

	return 0;
}

Test 10.27

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 4, 4, 5, 5, 6, 7, 8, 8, 8, 9 };
	list<int> lst;

	//如果使用普通的迭代器,要求lst中本来就有足够多的元素才能拷贝
	lst.assign(10, 0);
	unique_copy(vec.begin(), vec.end(), lst.begin());
    
	//如果使用插入迭代器,则lst可以是空的
	unique_copy(vec.begin(), vec.end(), front_inserter(lst));
	unique_copy(vec.begin(), vec.end(), back_inserter(lst));


	for (auto& i : lst)
		cout << i << " ";

	return 0;
}

Test 10.28

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	list<int> lst;

	//front_inserter
	copy(vec.begin(), vec.end(), front_inserter(lst));

	for (auto& i : lst)
		cout << i << " ";
	cout << endl;

	//back_inserter
	lst.clear();
	copy(vec.begin(), vec.end(), back_inserter(lst));

	for (auto& i : lst)
		cout << i << " ";
	cout << endl;

	//inserter
	lst.clear();
	copy(vec.begin(), vec.end(), inserter(lst, lst.begin()));

	for (auto& i : lst)
		cout << i << " ";
	cout << endl;

	return 0;
}

Output:

9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

Test 10.29

int main()
{
	//use istream_iterator read elements in infile.txt
	ifstream fin("infile.txt");
	istream_iterator<string> in_it(fin), in_eof;
	vector<string> svec;
	while (in_it != in_eof)
		svec.push_back(*in_it++);

	//use ostream_iterator output elements in ivec
	ostream_iterator<string> out_it(cout, " ");
	copy(svec.begin(), svec.end(), out_it);

	return 0;
}

Test 10.30

int main()
{
	//use istream_iterator read elements in infile.txt
	ifstream fin("infile.txt");
	istream_iterator<int> in_it(fin), in_eof;
	vector<int> vec;
	copy(in_it, in_eof, back_inserter(vec));
	sort(vec.begin(), vec.end());

	//use ostream_iterator output elements in ivec
	ostream_iterator<int> out_it(cout, " ");
	copy(vec.begin(), vec.end(), out_it);

	return 0;
}

Test 10.31

int main()
{
	//use istream_iterator read elements in infile.txt
	ifstream fin("infile.txt");
	istream_iterator<int> in_it(fin), in_eof;
	vector<int> vec;
	unique_copy(in_it, in_eof, back_inserter(vec));
	sort(vec.begin(), vec.end());

	//use ostream_iterator output elements in ivec
	ostream_iterator<int> out_it(cout, " ");
	copy(vec.begin(), vec.end(), out_it);

	return 0;
}

Test 10.33

void classifyint(const string& infile, const string& outfile1, const string& outfile2)
{
	//use istream_iterator read elements in infile.txt
	ifstream fin(infile);
	istream_iterator<int> in_it(fin), in_eof;
	vector<int> vec;
	copy(in_it, in_eof, back_inserter(vec));

	//use ostream_iterator output elements in ivec
	ofstream fout1(outfile1);
	ofstream fout2(outfile2);
	ostream_iterator<int> out1_it(fout1, " ");
	ostream_iterator<int> out2_it(fout2, "\n");
	for (auto& i : vec)
	{
		if (i % 2 != 0)
			out1_it = i;
		else
			out2_it = i;
	}
}
int main()
{
	classifyint("infile.txt", "outfile1.txt", "outfile2.txt");

	return 0;
}

Test 10.34

vector<int> vec{ 0, 1, 2, 3, 4 };

ostream_iterator<int> out_it(cout, " ");
copy(vec.crbegin(), vec.crend(), out_it);

Test 10.36

vector<int> vec{ 0, 1, 2, 3, 0, 4 };

int index = vec.crend() - find(vec.crbegin(), vec.crend(), 0) - 1;
cout << index << endl;

Test 10.37

vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> lst;
copy(vec.crbegin() + 3, vec.crend() - 2, back_inserter(lst));

//cout
ostream_iterator<int> out_it(cout, " ");
copy(lst.cbegin(), lst.cend(), out_it);
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值