10.1 概述
1 泛型算法:实现了一些经典算法的公共接口,如排序和搜索,可以用于不同类型的元素和多种容器类型
2 基本上都定义在algorithm和numeric两个头文件中
3 迭代器令算法不依赖于容器,依赖于元素类型的操作
练习10.1
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int t, n;
vector<int> vec;
cout << "请输入int序列个数:" << endl;
cin >> n;
cout << "请输入int序列:" << endl;
for (int i = 0;i < n;i++) {
cin >> t;
vec.push_back(t);
}
cout << "请输入要统计的值:" << endl;
int num;
cin >> num;
cout << count(vec.begin(), vec.end(), num) << endl;
system("pause");
return 0;
}
练习10.2
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int num = 0,t = 0;
string s1;
list <string> lst;
cout << "请输入字符串序列数量" << endl;
cin >> t;
cout << "请输入" << t << "个字符串" << endl;
for (int i = 0; i < t; i++){
cin >> s1;
lst.push_back(s1);
}
cout << "请输入要统计的字符串:" << endl;
string s2;
cin >> s2;
cout << count(lst.begin(), lst.end(), s2) << endl;
system("pause");
return 0;
}
10.2 初识泛型算法
1 标准库基本上都是对一个范围内的容器进行操作,所以参数中总会有两个表示范围的迭代器
2 对于读取而不改变元素的算法,最好使用cbegin()和cend(),否则用begin()和end()
练习10.3
#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
int main() {
vector<int> vec = { 1,2,3 };
int sum = accumulate(vec.cbegin(), vec.cend(), 0);
cout << sum << endl;
return 0;
}
练习10.4
将初值设定为0,表明返回值为int类型,使用之后,会将double转换为int,损失精度
练习10.5
equal会比较指针地址,而不是字符串值,比较的结果与string类型的不一致。
练习10.6
fill_n(vec.begin(),vec.size(),0);//注意其三个参数的意义
练习10.7
(a):lst和vec之间的大小未保证相同,vec.resize(lst.size)
(b):reverse是改变容器容量的,并没有改变其大小,用resize()
练习10.8
算法只是产生了一个插入迭代器,然后使用这个迭代器进行插入操作。
练习10.9
#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void elimDups(vector<string> &words) {
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
cout << "unique后:";
for (auto i : words) {
cout << i << " ";
}
cout << endl;
cout << "erase后:";
words.erase(end_unique, words.end());
for (auto i : words) {
cout << i << " ";
}
cout << endl;
}
int main() {
vector<string> words = { "I", "love", "you", "I", "love", "you", "I" };
elimDups(words);
system("pause");
return 0;
}
练习10.10
算法只作用于迭代器,而不直接对容器进行操作。
练习10.11
#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void elimDups(vector<string> &words) {
cout << "isShorter后:";
stable_sort(words.begin(), words.end(),isShorter);
auto end_unique = unique(words.begin(), words.end());
cout << "unique后:";
for (auto i : words) {
cout << i << " ";
}
cout << endl;
cout << "erase后:";
words.erase(end_unique, words.end());
for (auto i : words) {
cout << i << " ";
}
cout << endl;
}
int main() {
vector<string> words = { "I", "love", "you", "I", "love", "you", "I" };
elimDups(words);
system("pause");
return 0;
}
练习10.12
bool compareIsbn(const Sales_data &a, const Sales_data &b) {
return a.isbn() < b.isbn();
}
练习10.13
#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(const string &a) {
return a.size() >= 5;
}
int main() {
vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
partition(words.begin(), words.end(), cmp);
for (auto &i : words) {
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
练习10.14
#include<iostream>
using namespace std;
int main() {
auto f = [](int a,int b) {return a + b;};
cout << f(1, 2) << endl;
return 0;
}
练习10.15
#include<iostream>
using namespace std;
int main() {
int a = 1;
auto f = [a](int b) {return a + b;};
cout << f(2) << endl;
system("pause");
return 0;
}
练习10.16
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> &elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
return words;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(),
[](const string &a, const string &b)
{ return a.size() < b.size(); });
auto wc = find_if(words.begin(), words.end(),
[sz](const string &a)
{ return a.size() >= sz; });
auto count = words.end() - wc;
cout << count << endl;
for(const auto s : words)
cout << s << " ";
cout << endl;
}
int main()
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
biggies(vs, 5);
return 0;
}
练习10.17
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
class Sales_data
{
public:
Sales_data();
Sales_data(string s):_isbn(s)//列表初始化格式(:类内变量名(初始化值),)
{
}
string isbn()
{
return _isbn;
}
string _isbn;
};
int main(int argc, char**argv)
{
Sales_data a("because");//初始化对象
Sales_data b("I");
Sales_data c("Like");
Sales_data d("your");
Sales_data e("beautiful");
Sales_data f("eyes");
vector<Sales_data> vec1;//VS2010不支持列表初始化
vec1[0] = a;
vec1[1] = b;
vec1[2] = c;
vec1[3] = d;
vec1[4] = e;
vec1[5] = f;
stable_sort(vec1.begin(),vec1.end(),[](Sales_data s1, Sales_data s2){return s1.isbn().size() < s2.isbn().size();});//排序
cout<<"排序后的vector:";
for(int i = 0; i < vec1.size(); ++i)
{
cout<<vec1[i].isbn()<<" ";
}
return 0;
}
练习10.18-19
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> &elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
return words;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words);
auto wc = partition(words.begin(), words.end(),
[sz](const string &a)
{ return a.size() >= sz; });
//auto wc = stable_partition(words.begin(), words.end(),
//[sz](const string &a)
//{ return a.size() >= sz; });
auto count = words.end() - wc;
cout << count << endl;
for (const auto s : words)
cout << s << " ";
cout << endl;
}
int main()
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
biggies(vs, 5);
system("pause");
return 0;
}
练习10.20
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
string::size_type sz = 6;
auto wc = count_if(words.begin(), words.end(),
[sz](const string &a)
{ return a.size() >= sz; });
cout << wc << endl;
system("pause");
return 0;
}
练习10.21
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int v = 5;
auto f = [&v]()->bool
{
if (v <= 0) return false;
else {
--v;
return true;
}
};
while (f()) {
cout << v << endl;
}
system("pause");
return 0;
}