C++ Primer第五版_第十章习题答案(21~30)

news/2024/7/23 16:25:54 标签: c++, 开发语言, 算法

文章目录

      • 练习10.21
      • 练习10.22
      • 练习10.23
      • 练习10.24
      • 练习10.25
      • 练习10.26
      • 练习10.27
      • 练习10.28
      • 练习10.29
      • 练习10.30

练习10.21

编写一个 lambda,捕获一个局部 int 变量,并递减变量值,直至它变为0。一旦变量变为0,再调用lambda应该不再递减变量。lambda应该返回一个bool值,指出捕获的变量是否为0。

int i = 10;
auto f = [&i]() -> bool { return (i == 0 ? true : !(i--)); };
while (!f()) cout << i << endl;

练习10.22

重写统计长度小于等于6的单词数量的程序,使用函数代替 lambda。

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

using std::string;
using namespace std::placeholders;

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

int main()
{
	std::vector<string> authors{ "1234567", "1234", "1234567890", "1234567", "12345" };
	std::cout << count_if(authors.cbegin(), authors.cend(), bind(isLesserThanOrEqualTo6, _1, 6)) << std::endl;
}

练习10.23

bind 接受几个参数?

假设被绑定的函数接受 n 个参数,那么bind 接受 n + 1 个参数。

练习10.24

给定一个string,使用 bind 和 check_size 在一个 int 的vector 中查找第一个大于string长度的值。

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

using namespace std;
using namespace std::placeholders;

bool check_size(const string& s, size_t i)
{
	return i > s.size();
}

int main()
{
	vector<int> v = { 1, 2, 3, 4, 5, 6, 7 };
	string s("12345");

	auto it = find_if(v.cbegin(), v.cend(), bind(check_size, s, _1));
	if (it != v.end())
		cout << *it << endl;
}

练习10.25

在10.3.2节的练习中,编写了一个使用partition 的biggies版本。使用 check_size 和 bind 重写此函数。

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

using std::string; using std::vector;
using namespace std::placeholders;

void elimdups(vector<string> &vs)
{
	std::sort(vs.begin(), vs.end());
	vs.erase(unique(vs.begin(), vs.end()), vs.end());
}

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);
	auto iter = std::stable_partition(words.begin(), words.end(), bind(check_size, _1, sz));
	for_each(words.begin(), iter, [](const string &s) { std::cout << s << " "; });
}

int main()
{
	std::vector<std::string> v{ "the", "quick", "red", "fox", "jumps",
		"over", "the", "slow", "red", "turtle" };
	biggies(v, 4);

	return 0;
}

练习10.26

解释三种迭代器的不同之处。

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

练习10.27

除了 unique 之外,标准库还定义了名为 unique_copy 的函数,它接受第三个迭代器,表示拷贝不重复元素的目的位置。编写一个程序,使用 unique_copy将一个vector中不重复的元素拷贝到一个初始化为空的list中。

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

int main()
{
	std::vector<int> vec{ 1, 1, 3, 3, 5, 5, 7, 7, 9 };
	std::list<int> lst;

	std::unique_copy(vec.begin(), vec.end(), back_inserter(lst));
	for (auto i : lst)
		std::cout << i << " ";
	std::cout << std::endl;
	return 0;
}

练习10.28

一个vector 中保存 1 到 9,将其拷贝到三个其他容器中。分别使用inserter、back_inserter 和 front_inserter 将元素添加到三个容器中。对每种 inserter,估计输出序列是怎样的,运行程序验证你的估计是否正确。

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

using std::list; using std::copy; using std::cout; using std::endl;

template<typename Sequence>
void print(Sequence const& seq)
{
	for (const auto& i : seq)
		std::cout << i << " ";
	std::cout << std::endl;
}

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

	// inserter
	list<int> lst1;
	copy(vec.cbegin(), vec.cend(), inserter(lst1, lst1.begin()));
	print(lst1);

	// back_inserter
	list<int> lit2;
	copy(vec.cbegin(), vec.cend(), back_inserter(lit2));
	print(lit2);

	// front_inserter
	list<int> lst3;
	copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
	print(lst3);

	return 0;
}

练习10.29

编写程序,使用流迭代器读取一个文本文件,存入一个vector中的string里。

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

using std::string;

int main()
{
	std::ifstream ifs("H:/code/C++/Cpp_Primer_Answers/data/books.txt");
	std::istream_iterator<string> in(ifs), eof;
	std::vector<string> vec;
	std::copy(in, eof, back_inserter(vec));

	std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator<string>(std::cout, "\n"));
	return 0;
}

练习10.30

使用流迭代器、sort 和 copy 从标准输入读取一个整数序列,将其排序,并将结果写到标准输出。

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

using namespace std;

int main()
{
	vector<int> v;
	istream_iterator<int> int_it(cin), int_eof;

	copy(int_it, int_eof, back_inserter(v));
	sort(v.begin(), v.end());

	copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
	cout << endl;
	return 0;
}

http://www.niftyadmin.cn/n/195414.html

相关文章

【Linux 入门篇(二)】Ubuntu使用基础

目录一、终端基础命令二、软件安装1. 通过APP Store安装2. 使用APT工具安装3. deb软件包安装4. 自己下载程序源码编译安装5. 其他安装方法三、文件系统结构1. 跟目录“/”2. Ubuntu文件系统结构3. 绝对路径和相对路径四、磁盘管理1. 磁盘文件2. 磁盘和目录的容量查询命令3. 磁盘…

Java基础——Set集合实现类

&#xff08;1&#xff09;Set集合实现类特点&#xff1a; HashSet&#xff1a;无序&#xff0c;不重复&#xff0c;无索引。LinkedHashSet&#xff1a;有序&#xff0c;不重复&#xff0c;无索引。TreeSet&#xff1a;排序&#xff0c;不重复&#xff0c;无索引。&#xff08…

【华为OD机试真题】找数字(javapython)

找数字 知识点哈希表 时间限制:1s空间限制:256MB 限定语言:不限 题目描述: 给一个二维数组nums,对于每一个元素 num[i], 找出距离最近的且值相等的元素, 输出横纵坐标差值的绝对值之和,如果没有等值元素,则输出-1。 例如: 输入数组nums为 0 3 5 4 2 2 5 7 8 3 …

Python每日一练(20230408)

目录 1. 两数相除 &#x1f31f;&#x1f31f; 2. 分割回文串 &#x1f31f;&#x1f31f; 3. x 的平方根 &#x1f31f; &#x1f31f; 每日一练刷题专栏 &#x1f31f; Golang每日一练 专栏 Python每日一练 专栏 C/C每日一练 专栏 Java每日一练 专栏 1. 两数相除 …

JavaSE基础(25) 抽象类

程序是用来模拟现实世界、解决现实问题的&#xff1b; 现实世界中存在的都是“动物”具体的子类对象&#xff0c;并不存在“动物”对象&#xff0c;所以&#xff0c;Animal不应该被独立创建成对象。 将子类的共同特征进行抽取&#xff0c;抽象成一个父类。如果有的功能在父类…

Lambda表达式的使用

练习2&#xff1a; 定义一个接口(Flyable),里面定义一个抽象方法&#xff1a;void fly(String s); 定义一个测试类&#xff08;FlyableDemo&#xff09;&#xff0c;在测试类中提供两个方法 一个方法是&#xff1a;useFlyable(Flyable f) 一个方法是主方法&#xff0c;在主方…

Github成就解锁指南详细讲解

目录 前言 解锁方法详细讲解 Pair Extraordinaire Quickdraw YOLO Pull Shark Starstruck Galaxy Brain Public Sponsor Arctic Code Vault Contributor&#xff08;北极代码库贡献者&#xff09; Mars 2020 Contributor (火星代码库贡献者) 参考资料 前言 Github…

启动优化中的一些黑科技,了解一下~

作者&#xff1a;程序员江同学 前言 启动速度优化是 android 开发中的常见需求&#xff0c;除了一些常规的手段之外&#xff0c;也有一些黑科技手段&#xff0c;我们来看一下这些黑科技手段是否有效&#xff0c;以及如何实现 线程优先级设置 线程优先级设置的概念很容易理解…