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