C++ Primer(5e)第10章习题

10.1

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

using namespace std;

int main()
{
	vector<int> num;
	int val;
	cout << "Enter some numbers:" << endl;
	while (cin >> val)
		num.push_back(val);
	int temp = 1; // 给定值,随心而定
	auto result = count(num.begin(), num.end(), temp);
	cout << "The" << (result == 1 ? " time" : " times") << " of " << 
		temp << " is " << result << endl;
	return 0;
}

10.2

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

using namespace std;

int main()
{
	list<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	string val = "box";  // 随心而动,随刃而行
	auto result = count(s.begin(), s.end(), val);
	cout << "The " << (result == 1 ? " time" : " times") << " of "
		<< val << " is " << result << endl;
	return 0;
}

10.3

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

using namespace std;

int main()
{
	vector<int> num;
	int val;
	cout << "Enter some number:" << endl;
	while (cin >> val)
		num.push_back(val);
	auto sum = accumulate(num.cbegin(), num.cend(), 0);
	cout << "The sum is " << sum << endl;
	return 0;
}

10.4

/* 会造成精度丢失 */
#include<iostream>
#include<vector>
#include<numeric>

using namespace std;

int main()
{
	vector<double> v;
	double val;
	cout << "Enter some number:" << endl;
	while (cin >> val)
		v.push_back(val);
	auto sum = accumulate(v.cbegin(), v.cend(), 0);
	cout << "The sum is " << sum << endl;
	return 0;
}

10.5

冒得问题

10.6

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

using namespace std;

int main()
{
	vector<int> vec;
	cout << "Enter some numbers:" << endl;
	int val;
	while (cin >> val)
		vec.push_back(val);
	for (auto c : vec)
		cout << c << " ";
	cout << endl;
	fill(vec.begin(), vec.end(), 0);
	for (auto c : vec)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.7

(a)有错误,必须保证vec的序列至少与lst的序列一样长。
(b)有错误,vec中没有元素。

10.8

back_inserter接收一个指向容器的引用,返回一个与该容器绑定的插入迭代器。

10.9

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

using namespace std;

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	for (auto c : s)
		cout << c << " ";
	cout << endl;
	sort(s.begin(), s.end());
	auto end_unique = unique(s.begin(), s.end());
	for (auto c : s)
		cout << c << " ";
	cout << endl;
	s.erase(end_unique, s.end());
	for (auto c : s)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.10

迭代器令算法不依赖于容器

10.11

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

using namespace std;

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

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> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	elimDups(s);
	stable_sort(s.begin(), s.end(), isShorter);
	for (const auto &c : s)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.13

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

using namespace std;

bool overFive(const string &s)
{
	if (s.size() >= 5)
		return true;
	else
		return false;
}

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	auto it = partition(s.begin(), s.end(), overFive); // 算法返回一个迭代器
	for (auto iter = s.begin(); iter != it; ++iter)
		cout << *iter << " ";
	cout << endl;
	return 0;
}

10.14

auto f = [] (int x, int y) { return x + y; };

10.15

auto f = [f()] (int y) { return (f() + y); };

10.16

/* 把书上的敲了一遍~ */
#include<iostream>
#include<vector>
#include<algorithm>
#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());
}

string make_plural(size_t ctr, const string &word, const string &ending)
{
	return (ctr > 1) ? word + ending : word;
}

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;
}

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	biggies(s, 5);
	return 0;
}

10.18

#include<iostream>
#include<vector>
#include<algorithm>
#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());
}

string make_plural(size_t ctr, const string &word, const string &ending)
{
	return (ctr > 1) ? word + ending : word;
}

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(),  //用partition
		[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;
}

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	biggies(s, 5);
	return 0;
}

10.19

#include<iostream>
#include<vector>
#include<algorithm>
#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());
}

string make_plural(size_t ctr, const string &word, const string &ending)
{
	return (ctr > 1) ? word + ending : word;
}

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(),    // 用stable_partition
		[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;
}

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	biggies(s, 5);
	return 0;
}

10.20

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

using namespace std;

bool overSix(const string &s)
{
	if (s.size() > 6)
		return true;
	else
		return false;
}

int main()
{
	vector<string> s;
	string word;
	cout << "Enter some strings:" << endl;
	while (cin >> word)
		s.push_back(word);
	auto cnt = count_if(s.begin(), s.end(), overSix);
	cout << "The " << (cnt == 1 ? "time " : "times ") << "is " << cnt << endl;
	return 0;
}

10.21

不清楚该题是不是有问题,它说捕获已给局部int变量,递减变量值,使之成为0;但是如果这个局部int变量是负数呢? 递减就不可能成为0。。。所以,我觉得捕获一个unsigned 变量好些,如果有道友明白其中缘由,还望不吝赐教~

size_t x = 42; // 随便值
auto f = [x]() -> bool { if (x > 0) --x; else return x; };

10.26

back_inserter:在容器尾部插入元素
front_inserter:在容器首部插入元素
inserter:在给定迭代器所表示的元素之前插入元素

10.27

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

using namespace std;

int main()
{
	vector<string> s1;
	list<string> s2;
	string val;
	cout << "Enter some strings:" << endl;
	while (cin >> val)
		s1.push_back(val); 
	sort(s1.begin(), s1.end());
	unique_copy(s1.begin(), s1.end(), back_inserter(s2));
	for (auto c : s2)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.28

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

using namespace std;

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

10.29

#include<iostream>
#include<iterator>
#include<string>
#include<vector>

using namespace std;

int main()
{
	vector<string> s;
	istream_iterator<string> str_in(cin), eof;
	while (str_in != eof)
		s.push_back(*str_in++);
	for (auto &c : s)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.30

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

using  namespace std;

int main()
{
	istream_iterator<int> in_iter(cin), eof;
	ostream_iterator<int> out_iter(cout, " ");
	vector<int> num;
	while (in_iter != eof)
		num.push_back(*in_iter++);
	sort(num.begin(), num.end());
	copy(num.begin(), num.end(), out_iter);
	cout << endl;
	return 0;
}

10.31

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

using  namespace std;

int main()
{
	istream_iterator<int> in_iter(cin), eof;
	ostream_iterator<int> out_iter(cout, " ");
	vector<int> num;   
	while (in_iter != eof)
		num.push_back(*in_iter++);
	sort(num.begin(), num.end());
	unique_copy(num.begin(), num.end(), out_iter);
	cout << endl;
	return 0;
}

10.34

#include<iostream>
#include<vector>
#include<iterator>

using namespace std;

int main()
{
	vector<int> v = { 1, 2, 3, 4 };
	vector<int>::reverse_iterator rbeg = v.rbegin(), rend = v.rend();
	while (rbeg != rend)
		cout << *rbeg++ << " ";
	cout << endl;
	return 0;
}

10.35

#include<iostream>
#include<vector>

using namespace std;

int main()
{
	vector<int> v;
	int val;
	cout << "Enter some numbers:" << endl;
	while (cin >> val)
		v.push_back(val);
	for (auto r_iter = v.crbegin(); r_iter != v.crend(); ++r_iter)
		cout << *r_iter << " ";
	cout << endl;
	return 0;
}

10.36

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

using namespace std;

int main()
{
	list<int> v;
	int val;
	cout << "Enter some numbers:" << endl;
	while (cin >> val)
		v.push_back(val);
	auto num = find(v.crbegin(), v.crend(), 0);
	cout << *(++num) << endl;
	return 0;
}

10.37

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

using namespace std;

int main()
{
	vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	list<int> lst;
	for (auto r_iter = v.crbegin() + 2; r_iter != v.crbegin() + 7; ++r_iter)
		lst.push_back(*r_iter);
	for (auto &c : lst)
		cout << c << " ";
	cout << endl;
	return 0;
}

10.38

输入迭代器:只用于顺序访问
输出迭代器:用于推进迭代器的前置和后置递增运算++;解引用运算符*
前向迭代器:支持所有输入和输出迭代器的操作,而且可以多次读写同一个元素
双向迭代器:除了支持所有前向迭代器的操作之外,双向迭代器还支持前置和后置递减运算符
随机访问迭代器:支持双向迭代器所有功能,还支持用于比较两个迭代器相对位置的关系运算符;迭代器和一个整数值的加减运算;用于迭代器上的减法运算符及下标运算符。

10.39

list上的迭代器属于双向迭代器,vector属于随机访问迭代器

10.40

copy要求输入和输出迭代器;reverse和unique要求双向迭代器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值