C++Primer第五版——習題答案詳解(九)


習題答案目錄:https://www.cnblogs.com/Mered1th/p/10485695.htmlhtml

第10章 泛型算法


練習10.1ios

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

using namespace std;

int main() {
	int t, n;
	vector<int> vec;
	cout << "請輸入序列個數:" << endl;
	cin >> n;
	cout << "請輸入序列:" << endl;
	for (int i = 0;i < n;i++) {
		cin >> t;
		vec.push_back(t);
	}
	cout << "請輸入要統計的值:" << endl;
	int num;
	cin >> num;
	cout << count(vec.begin(), vec.end(), num) << endl;
	

	system("pause");
	return 0;
}

練習10.2算法

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

using namespace std;

int main() {
	int n;
	string t;
	list<string> lst;
	cout << "請輸入字符串個數:" << endl;
	cin >> n;
	cout << "請輸入" << n << "個字符串:" << endl;
	for (int i = 0;i < n;i++) {
		cin >> t;
		lst.push_back(t);
	}
	cout << "請輸入要統計的字符串:" << endl;
	string num;
	cin >> num;
	cout << count(lst.begin(), lst.end(), num) << endl;

	system("pause");
	return 0;
}

練習10.3函數

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

using namespace std;

int main() {
	vector<int> vec = { 1,2,3,4,5 };
	int sum = accumulate(vec.begin(), vec.end(), 0);
	cout << sum << endl;
	return 0;
}

練習10.4 初始值設爲0表示返回值爲int類型,會有精度損失ui

練習10.5 equal會比較指針地址,而不是字符串值,比較的結果與string類型的不一致。spa

練習10.6指針

fill_n(vec.begin(),vec.size(),0);

練習10.7 a.lst和vec之間的大小未保證相同,vec.resize(lst.size) b.vec.resize(10);code

練習10.9htm

#include<iostream>
#include<numeric>
#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());
	cout << "unique後:";
	for (auto i : words) {
		cout << i << " ";
	}
	cout << endl;
	cout << "erase後:";
	words.erase(end_unique, words.end());
	for (auto i : words) {
		cout << i << " ";
	}
	cout << endl;
}

int main() {
	vector<string> words = { "abc","abc","abc","bcd","efg","bcd","eqd" };
	elimDups(words);
	system("pause");
	return 0;
}

練習10.10 標準庫算法對迭代器而不是容器進行操做。blog

練習10.11

#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
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> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	elimDups(words);
	stable_sort(words.begin(), words.end(), isShorter);
	for (auto &i : words) {
		cout << i << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

練習10.12

bool compareIsbn(const Sales_data &a, const Sales_data &b) {
        return a.isbn() < b.isbn();
}

練習10.13

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

bool cmp(const string &a) {
	return a.size() >= 5;
}

int main() {
	vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
	partition(words.begin(), words.end(), cmp);
	for (auto &i : words) {
		cout << i << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

練習10.14

#include<iostream>
using namespace std;
int main() {
	auto f = [](int a,int b) {return a + b;};
	cout << f(1, 2) << endl;
	return 0;
}

練習10.15

#include<iostream>
using namespace std;

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

練習10.16

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

using namespace std;

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

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 << endl;
	for(const auto s : words)
		cout << s << " ";
	cout << endl;
}

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

	biggies(vs, 5);

	return 0;
}

練習 10.18-10.19

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

using namespace std;

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

void biggies(vector<string> &words, vector<string>::size_type sz)
{
	elimDups(words);
	auto wc = partition(words.begin(), words.end(),
		[sz](const string &a)
	{ return a.size() >= sz; });
    
//auto wc = stable_partition(words.begin(), words.end(),
//[sz](const string &a)
//{ return a.size() >= sz; });

	auto count = words.end() - wc;
	cout << count << endl;
	for (const auto s : words)
		cout << s << " ";
	cout << endl;
}

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

	biggies(vs, 5);
	system("pause");
	return 0;
}

練習10.20

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

using namespace std;

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

練習10.21

#include<iostream>
#include<algorithm>

using namespace std;

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

練習10.22

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

using namespace std;

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

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

	system("pause");
	return 0;
}

練習10.23

假設要綁定的函數有n個參數,綁定取n + 1個參數。另一個是函數自己的綁定。

練習10.24

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

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

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

	auto iter = find_if(vi.begin(), vi.end(), bind(check_size, s, placeholders::_1));

	cout << *iter << endl;
	system("pause");
	return 0;
}

練習10.25

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

using namespace std;

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

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

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

	auto iter = partition(vs.begin(), vs.end(), bind(check_size, placeholders::_1, 5));
	for (const auto s : vs)
		cout << s << " ";
	cout << endl;

	vs.erase(iter, vs.end());
	for (const auto s : vs)
		cout << s << " ";
	cout << endl;
	system("pause");
	return 0;
}

後續部分以後再更新。。

相關文章
相關標籤/搜索