c++primer第五版课后练习答案(第十章)

本文深入探讨了C++编程的高级技巧与实用实例,涵盖了从基础语法到复杂算法的全面指南,帮助开发者提高编程效率并解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

chapter10_10.1

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec = {1,2,3,2,5,6};
	int num = 0;
	num = count(ivec.cbegin(), ivec.cend(),2);
	cout << num << endl;
	return 0;
}


chapter10_10.2

int _tmain(int argc, _TCHAR* argv[])
{
	list<string> svec = { "student","name","master","student","hello" ,"student"};
	int num = 0;
	num = count(svec.cbegin(), svec.cend(), "student");
	cout << num << endl;
	return 0;
}
chapter10_10.3

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec = { 1, 2, 3 };
	int sum ;
	sum = accumulate(ivec.cbegin(), ivec.cend(), 0);
	cout << sum << endl;
	return 0;
}

chapter10_10.6

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec = { 1, 2, 3, 2, 5, 6 };
	fill_n(ivec.begin(),ivec.size(),0); //值被赋予迭代器指向的元素

	fill_n(back_inserter(ivec), ivec.size(), 2);  //一个与赋值号相等的元素被添加到容器中

	for (auto it = ivec.cbegin(); it != ivec.cend(); it++)
		cout << *it << endl;
	return 0;
}


chapter10_10.9


#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &words)
{
	cout << "读取输入后打印内容:" << endl;
	for (auto it = words.begin(); it != words.end(); it++)
		cout<< *it << " ";
	sort(words.begin(), words.end());
	cout << endl << "读取调用sort后打印内容:" << endl;
	for (auto it = words.begin(); it != words.end(); it++)
		cout << *it << " ";
	auto end_unique = unique(words.begin(), words.end());
	cout << endl<<"读取调用unique后打印内容:" << endl;
	for (auto it = words.begin(); it != words.end(); it++)
		cout << *it << " ";
	words.erase(end_unique, words.end());
	cout << endl<<"读取调用erase后打印内容:" << endl;
	for (const auto &it : words)
		cout << it << " ";
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<string> svect = { "the", "qucik", "red", "fox","jumps","over", "the", "slow", "red" , "turtle" };
	elimDups(svect);
	cout << endl;
	return 0;
}

chapter10_10.9

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
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 _tmain(int argc, _TCHAR* argv[])
{
	vector<string> svect={ "the", "qucik", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	elimDups(svect);
	stable_sort(svect.begin(), svect.end(), isShorter);
	for (auto it = svect.begin(); it != svect.end(); it++)
		cout << *it << " ";
	cout << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值