习题10.1-10.2
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vec;
list<string> lis;
int buf_int;
string buf_str;
cout << "Please input some value to vec" << endl;
while (cin >> buf_int ) {
if (buf_int == -1)
break;
vec.push_back(buf_int);
}
cout << "Please input some string to lis" << endl;
while (cin >> buf_str) {
if (buf_str == "-1")
break;
lis.push_back(buf_str);
}
cout << "Please input a value which you want find in the vec" << endl;
int vaule;
cin >> vaule;
auto num_value = count(vec.begin(), vec.end(), vaule);
cout << "The num of value in the vec is " << num_value << endl;
cout << "Please input a string which you want find in the lis" << endl;
string str;
cin >> str;
auto num_str = count(lis.begin(), lis.end(), str);
cout << "The num of string in the lis is " << num_str << endl;
system("pause");
return 0;
}
习题10.3
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vec = {1,2,3,4,5,6,7,8};
int sum = accumulate(vec.begin(),vec.end(),0);
cout << sum << endl;
system("pause");
return 0;
}
习题10.4
如果第三个形参是int,那么accumulate的结果也是int。
习题10.5
总会返回0,对比C风格字符串,应该使用strcmp
习题10.6
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vec = {1,2,3,4,5,6,7,8};
fill_n(vec.begin(),vec.size(),0);
for (auto a : vec)
{
cout << a << " ";
}
system("pause");
return 0;
}
习题10.7
(a)vec的size为0,无法拷贝
(b)vec使用reserve后,该vec的最大容纳变为10,但size依旧是0.
习题10.8
back_inserter被包含在iterator中,调用插入迭代器,相当于调用一次push_back。
fill_n(vec.begin(),10,0)
fill_n(back_inserter(vec),10,0)
上述区别就在于,第二项是向vec插入十个0。而第一项是将vec中前是个元素置为0.
习题10.9
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
template <typename Sequence> auto println(Sequence const& seq) -> std::ostream &
{
for (auto const& elem : seq) std::cout << elem << " ";
return std::cout << std::endl;
}
auto eliminate_duplicates(std::vector<std::string>& vs)
-> std::vector<std::string> &
{
std::sort(vs.begin(), vs.end());
println(vs);
auto new_end = std::unique(vs.begin(), vs.end());
println(vs);
vs.erase(new_end, vs.end());
return vs;
}
int main()
{
std::vector<std::string> vs{ "a", "v", "a", "s", "v", "a", "a" };
println(vs);
println(eliminate_duplicates(vs));
system("pause");
return 0;
}
习题10.10
实际上,algorithms操作的是iterator而不是容器本身。仅通过迭代器,无法改变容器的大小。
习题10.11
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
template <typename Sequence> auto println(Sequence const& seq) -> std::ostream &
{
for (auto const& elem : seq) std::cout << elem << " ";
return std::cout << std::endl;
}
auto eliminate_duplicates(std::vector<std::string>& vs)
-> std::vector<std::string> &
{
std::sort(vs.begin(), vs.end());
auto new_end = std::unique(vs.begin(), vs.end());
vs.erase(new_end, vs.end());
return vs;
}
bool isShorter(const string &str1, const string &str2)
{
return str1.size() < str2.size();
}
int main()
{
std::vector<std::string> vs{ "aQ", "v12", "a22", "s222", "v22", "a1", "a1" };
eliminate_duplicates(vs);
stable_sort(vs.begin(), vs.end(), isShorter);
println(vs);
system("pause");
return 0;
}
习题10.12
略
习题10.13
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool isLonger(const string &str)
{
return str.size() > 5;
}
int main()
{
std::vector<std::string> vs{ "aQ1111", "v121111", "a221111", "s222", "v22", "a1", "a1" };
auto newEnd = partition(vs.begin(), vs.end(), isLonger);
for (auto it = vs.begin(); it != newEnd; it++)
{
cout << *it << " ";
}
cout << endl;
system("pause");
return 0;
}