#include <iostream>
#include<numeric>
#include<vector>
#include<list>
#include<algorithm>
#include<functional>
using namespace std;
class TwiceOver{
public:
bool operator()(int val1,int val2){
return val1==val2/2?true:false;
}
};
class Even{
public:
bool operator()(int val){return !(val%2);}
};
int main()
{
int ia[8]={1,3,6,10,15,21,28,36};
vector<int> vec(ia,ia+8);
list<int> ilist(ia,ia+8);
string sa[10]={"The","light","untonsured","hair",
"grained","and","hued","like","pale","oak"};
vector<string> svec(sa,sa+10);
list<string> slist(sa,sa+10);
//元素累加
int res=accumulate(ia,ia+8,0);//第三参数表示初值
int acc=accumulate(ilist.begin(),ilist.end(),0,plus<int>());
//相邻元素差额
list<int> iresult;//存放结果
adjacent_difference(ilist.begin(),ilist.end(),iresult.begin());//相减
adjacent_difference(ilist.begin(),ilist.end(),iresult.begin(),multiplies<int>());//相邻元素相乘
//查找相邻的重复元素int piter=
auto piter= adjacent_find(sa,sa+8);//返回一个iterator 指向重复元素的第一个
vector<int>::iterator iter= adjacent_find(vec.begin(),vec.end(),TwiceOver());
//二分搜索
int value=3;//需要查找的值
bool found_it=binary_search(ilist.begin(),ilist.end(),value);
found_it=binary_search(vec.begin(),vec.end(),value,greater<int>());//将值排成降序 再查找
//复制 将第一个容器的元素 一一 复制到第二个容器
vector<string> target(svec.size());
copy(svec.begin(),svec.end(),target.begin());
//逆向复制
copy(svec.begin(),svec.end(),target.begin());
//计数
//返回容器中与指定值相等元素个数vec.end()
// int count=count(svec.begin(),svec.end(),"and");
//在特定条件下计数
// 统计了数组中元素值小于10的元素个数,使用bind2nd将less仿函数的第二个参数绑定为10。
int ires= count_if(ia,ia+8,bind2nd(less<int>(),10));
ires=count_if(ilist.begin(),ilist.end(),Even());
equal();//判断相等与否
fill()//改填充容器的值
fill_n()//改填充容器的值 ,n次
find();//搜索
find_if();//在特定条件下搜索
find_end();//搜索某个子序列的最后一次出现的地方
find_first_of();搜索某个子序列的第一次出现的地方
for_each();//对范围内的第一个元素应用某个操作 遍历
generate();//以指定操作的运算结果填充特定范围内的元素
generate_n();//以指定操作的运算结果填充特定范围内的n个元素
includes();//涵盖于
inner_product();//内积
inplace_merge();//合并并取代
iter_swap();//元素互换
lexicographical_compare();//以字典排列方式做比较
max();min();//最值
max_element();//最大值的索引
merge();//合并两个序列
nth_element();//重新安排序列中第n个元素的左右两端
partial_sort();//局部排列
partial_sort_copy(); //局部排列并复制到其他地方
partial_sum();//局部总和
partition();//切割
stable_partition();//切割并保持元素之间的顺序
random_shuffle();//随机重排列
remove();//移除某种元素(但不删除)
remove_copy();//移除某种元素并将结果复制到另一个容器
remove_if();//有条件地移除某种元素
remove_copy_if();//有条件地移除某种元素并将结果复制到另一个容器
replace();//取代某种元素
replace_copy();//取代某种元素并将结果复制到另一个容器
replace_if();//有条件地取代某种元素
replace_copy_if();//有条件地取代某种元素并将结果复制到另一个容
reverse();//颠倒元素次序
reverse_copy();
rotate();//旋转
rotate_copy();
search();//搜索某个子序列
search_n();//搜索“连续发生n次的”子序列
set_difference();//差集
set_union();//并集
set_intersection();//交集
set_symmetric_difference();//对称差集
sort();//排序
stable_sort();
transform();//以两个序列为基础,交互作用产生第三个序列
unique();//使重复元素折叠并使其唯一
unique_copy();
cout << "Hello World!" << endl;
return 0;
}
C++ 常用泛型算法
最新推荐文章于 2024-07-25 21:43:46 发布