c++ primer 读书笔记之第十章 泛型算法
2017-07-02 12:14
232 查看
c++的算法见algorithm.cpp和numeric.cpp文件中。泛型是指这些算法,适用于不同的容器类型,和容器元素类型。
算法1:查找一个元素中是否含有某元素:find
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<int> v={1,2,3};
auto index=find(v.cbegin(),v.cend(),4);
cout << (index==v.cend() ? "no exist" : "presence");
return 0;
}
输出:
no exist
泛型算法分为三种:只读容器元素,写容器元素,重排容器元素
只读容器元素:
输出:
a a a
例1:求得容器内指定范围内元素的和:accumulate(包含于头文件numeric.cpp)
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<int> v={1,2,3};
int sum=accumulate(v.cbegin(),v.cend(),0);
cout << sum;
return 0;
}
输出:
6
其中第三个参数是初值,它的数据类型告诉系统使用哪种类型的"+"。为什么要强调这一点,看下面一个例子:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<string> v={"1","2","3"};
cout<<accumulate(v.cbegin(),v.cend(),string(""));
return 0;
}
是0k的
但下面的写法就是不ok的。因为“”常量字符串的数据类型是const char * 。这种数据类型是没有+运算符的。
例2:比较两个容器是否相等:equal(接受三个迭代器)
写容器元素:
例1:将序列指定范围中序列元素替代为某值:fill
例2:fill_n
例3:fill_n与back_inserter配合实现向一个空的容器插入元素
输出:
例4:copy算法,适用于不想改变原序列的情况
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<string> v1={"1","2","3"};
vector<string> v2;
replace_copy(v1.cbegin(),v1.cend(),back_inserter(v2),"1","w");
cout<<"v1:"<<endl;
for(auto i=v1.begin();i!=v1.end();i++)
cout<<*i<<" ";
cout<<endl<<"v2:"<<endl;
for(auto i=v2.begin();i!=v2.end();i++)
cout<<*i<<" ";
return 0;
}
输出结果:
注意:替代只可以相同大小的元素替代,例如“w”可以替代"1";但不可以"ww"来替代“1”
重排容器中元素
例1:去处容器中重复元素:sort(泛型算法)+unique(泛型算法)+erase(被容器对象所调用函数)
输出结果:
注意1:unique的输入是有序的,实质上它执行的只是一个相邻重复元素的覆盖
注意2:unique返回的是一个迭代器
算法1:查找一个元素中是否含有某元素:find
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<int> v={1,2,3};
auto index=find(v.cbegin(),v.cend(),4);
cout << (index==v.cend() ? "no exist" : "presence");
return 0;
}
输出:
no exist
泛型算法分为三种:只读容器元素,写容器元素,重排容器元素
只读容器元素:
#include <iostream> #include <vector> #include <numeric> #include <algorithm> #include <string> using namespace std; int main() { vector<string> v={"1","2","3"}; fill(v.begin(),v.end(),"a"); for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" "; return 0; }
输出:
a a a
例1:求得容器内指定范围内元素的和:accumulate(包含于头文件numeric.cpp)
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<int> v={1,2,3};
int sum=accumulate(v.cbegin(),v.cend(),0);
cout << sum;
return 0;
}
输出:
6
其中第三个参数是初值,它的数据类型告诉系统使用哪种类型的"+"。为什么要强调这一点,看下面一个例子:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<string> v={"1","2","3"};
cout<<accumulate(v.cbegin(),v.cend(),string(""));
return 0;
}
是0k的
但下面的写法就是不ok的。因为“”常量字符串的数据类型是const char * 。这种数据类型是没有+运算符的。
vector<string> v={"1","2","3"}; cout<<accumulate(v.cbegin(),v.cend(),"");
例2:比较两个容器是否相等:equal(接受三个迭代器)
vector<string> v={"1","2","3"}; vector<string> t={"1","2","3"}; cout<<equal(v.cbegin(),v.cend(),t.cbegin());
写容器元素:
例1:将序列指定范围中序列元素替代为某值:fill
#include <iostream> #include <vector> #include <numeric> #include <algorithm> #include <string> using namespace std; int main() { vector<string> v={"1","2","3"}; fill(v.begin(),v.end(),"a"); for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" "; return 0; }
例2:fill_n
vector<string> v={"1","2","3"}; fill_n(v.begin(),3,"0");
例3:fill_n与back_inserter配合实现向一个空的容器插入元素
#include <iostream> #include <vector> #include <numeric> #include <algorithm> #include <string> using namespace std; int main() { vector<string> v; fill_n(back_inserter(v),3,"0"); for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" "; return 0; }
输出:
0 0 0
例4:copy算法,适用于不想改变原序列的情况
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <string>
using namespace std;int main()
{
vector<string> v1={"1","2","3"};
vector<string> v2;
replace_copy(v1.cbegin(),v1.cend(),back_inserter(v2),"1","w");
cout<<"v1:"<<endl;
for(auto i=v1.begin();i!=v1.end();i++)
cout<<*i<<" ";
cout<<endl<<"v2:"<<endl;
for(auto i=v2.begin();i!=v2.end();i++)
cout<<*i<<" ";
return 0;
}
输出结果:
v1: 1 2 3 v2: w 2 3
注意:替代只可以相同大小的元素替代,例如“w”可以替代"1";但不可以"ww"来替代“1”
重排容器中元素
例1:去处容器中重复元素:sort(泛型算法)+unique(泛型算法)+erase(被容器对象所调用函数)
#include <iostream> #include <vector> #include <algorithm> #include <numeric> using namespace std; int main() { vector<int> v ={1,2,3,4,5,6,1,1,4,}; sort(v.begin(),v.end()); auto beg=unique(v.begin(),v.end()); v.erase(beg,v.end()); for(auto it=v.begin();it!=v.end();it++) { cout<<*it; } return 0; }
输出结果:
123456
注意1:unique的输入是有序的,实质上它执行的只是一个相邻重复元素的覆盖
注意2:unique返回的是一个迭代器
相关文章推荐
- c++ primer(第五版)笔记 第十章 泛型算法(2)
- c++ primer(第五版)学习笔记及习题答案代码版(第十章)泛型算法
- 读书笔记:C++ primer 5th edition--chapter10.泛型算法
- C++ Primer 读书笔记 - 第十章
- C++ Primer 读书笔记:第11章 泛型算法
- 《C++ Primer》读书笔记第十章-1-初识泛型算法 And Lambda
- 《C++ Primer》读书笔记第十章-2-再探迭代器 And 泛型算法结构
- C++ Primer 读书笔记 Charpter 11 泛型算法
- C++ Primer学习之(11)——泛型算法
- 《C++ Primer》读书笔记第一章
- 《C++ Primer》读书笔记(7.7)
- 『C程序设计』读书笔记系列文章之第十章 结构体与共用体
- 《C++ Primer》读书笔记-第三章 06 多维数组
- 《C++ Primer 5th》读书笔记5---重读IO标准库
- C++ primer 读书笔记
- C++ Primer 读书笔记——第12章 类
- 《C++ Primer》读书笔记-第九章 06 容器适配器
- C++ Primer 第五版读书笔记
- 读书笔记_java设计模式深入研究 第十章 命令模式 Command
- 第十章泛型算法