书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社
编译器 : win10 && VS2015
10.1
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
vector intVec = { 0,1,5,3,4,5,6,5 };
auto result = find(intVec.begin(), intVec.end(), 5);
string s = (result == intVec.end()) ? "未找到" : "找到了";
cout << s << *result << endl;
int nCount = count(intVec.begin(), intVec.end(), 5);
cout << "这个容器中有" << nCount << "个" << 5 << endl;
system("pause");
return 0;
}
10.2
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
vector strVec = { "can","you","find","the","right","word","right" };
int nCount = count(strVec.begin(), strVec.end(), "right");
cout << "这个容器中有" << nCount << "个right" << endl;
system("pause");
return 0;
}
10.3
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
vector intVec = { 0,11,22,33,44,55,66,77 };
int nCount = accumulate(intVec.begin(), intVec.end(), 0);
cout << "这个容器中所有元素和为" << nCount << endl;
system("pause");
return 0;
}
10.4
编译器可以编译通过,但可能会造成结果丢失;
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
vector intVec = { 0,11.8,22.8,33.8,44.8,55.8,66,77 };
int nCount = accumulate(intVec.begin(), intVec.end(), 0);
cout << "这个容器中所有元素和为" << nCount << endl;
system("pause");
return 0;
}
例如这段代码,正确结果为312,但是输出结果为308;
10.5
const char * 不支持 “==” 比较
10.6
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char** argv)
{
vector iVec = {1,2,3,4,5,6,7,8,9};
cout << "初始值为:" << endl;
for (auto i = iVec.begin(); i != iVec.end(); i++)
{
cout << *i << " ";
}
int n = iVec.size();
fill_n(iVec.begin(), n, 0);
cout << "替换后:" << endl;
for (auto i = iVec.begin(); i != iVec.end(); i++)
{
cout << *i << " ";
}
system("pause");
return 0;
}
10.7
a. 当list中元素个数大于一个时,因为vec是空的,程序会报错。当list中元素个数为0时,程序正常运行,但是list与vector中都是空的。
b. reserve只是预先分配内存空间大小,无法改变容器中元素数量,所以fill_n时还是会报错。
10.8
back_inserter是插入迭代器,是一种用来向容器中添加元素的迭代器。所以算法并没有改变容器元素个数。
10.9
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void elimDups(vector& words)
{
cout << "初始容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
sort(words.begin(), words.end());
cout << endl;
cout << "排序后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
auto end_unique = unique(words.begin(), words.end());
cout << endl;
cout << "重新排列后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
words.erase(end_unique, words.end());
cout << endl;
cout << "去重后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
}
int main(int argc, char** argv)
{
vector strVec = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(strVec);
system("pause");
return 0;
}
10.10
因为算法本身不会执行容器的操作,他们只会运行于迭代器之上,执行迭代器的操作。
10.11
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void elimDups(vector& words)
{
cout << "初始容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
sort(words.begin(), words.end());
cout << endl;
cout << "排序后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
auto end_unique = unique(words.begin(), words.end());
cout << endl;
cout << "重新排列后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
words.erase(end_unique, words.end());
cout << endl;
cout << "去重后容器为:" << endl;
for (auto i = words.begin(); i != words.end(); i++)
{
cout << *i << " ";
}
}
bool isShorter(const string& s1, const string& s2)
{
return s1.size() < s2.size();
}
int main(int argc, char** argv)
{
vector strVec = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(strVec);
stable_sort(strVec.begin(), strVec.end(), isShorter);
cout << endl;
cout << "在按长度排序后:" << endl;
for (auto &s : strVec)
{
cout << s << " ";
}
cout << endl;
system("pause");
return 0;
}
10.12
bool compareIsbn(Sales_data& sd1, Sales_data& sd2)
{
return sd1.isbn() > sd2.isbn();
}
Sales_data在另一个工程里,懒得导入了,就是把上面的isshorter换成这个
10.13
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace std;
bool stringLengthBiggerThanFive(string& s)
{
return s.size() >= 5;
}
int main(int argc, char** argv)
{
vector strVec = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
cout << "初始时容器为:" << endl;
for (auto &s : strVec)
{
cout << s << " ";
}
cout << endl;
auto end_pare = partition(strVec.begin(), strVec.end(), stringLengthBiggerThanFive);
strVec.erase(end_pare, strVec.end());
cout << "在按长度是否大于5排序后:" << endl;
for (auto &s : strVec)
{
cout << s << " ";
}
cout << endl;
system("pause");
return 0;
}
10.14