练习10.3:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
int main()
{
vector<int>v = { 1,2,3,4,5,6,7,8,9 };
int sum = accumulate(v.cbegin(), v.cend(), 0);
//求和
cout << sum << endl;
system("pause");
return 0;
}
练习10.4:accumulate的第三个参数就是其返回类型,直接使用0会导致精度丢失,但不会报错
练习10.5:两容器都保存const char* 类型可以进行比较:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
int main()
{
vector<const char*>vs1 = { "hello","world","666" };
list<const char* >vs2 = { "hello","world","666" };
bool compare = equal(vs1.cbegin(), vs1.cend(), vs2.cbegin());
cout << compare << endl;
system("pause");
return 0;
}