for_each()算法
for_each(beg,end,op1) // op1可以变动元素,op1的任何返回值都会被忽略
for_each()算法有一个独门绝技,那就是可以返回其仿函数(即一个类对象)
非变动性算法
元素计数:
count(beg,end,value);
count_if(beg,end,op1);
最大值和最小值:
min_element(beg,end);
min_element(beg,end,op2);
max_element(beg,end);
max_element(beg,end,op2);
搜寻第一个匹配元素:
find(beg,end,value);
find_if(beg,end,op1);
搜寻前n个连续匹配值:
search_n(beg,end,n,value);
search_n(beg,end,n,value,op2);
搜寻第一个子区间:
search(beg,end,searchbeg,searchend)
search(beg,end,searchbeg,searchend,op2)
搜寻最后一个子区间:
find_end(beg,end,searchbeg,searchend);
find_end(beg,end,searchbeg,searchend,op2);
搜寻 某些元素的第一次出现点: //可以使用逆向迭代器搜寻最后一个
find_first_of(beg,end,searchbeg,searchend);
find_first_of(beg,end,searchbeg,searchend,op2);
搜寻两个连续且相等的元素:
adjacent_find(beg,end)
adjacent_find(beg,end,op2);
区间的比较:
检验相等性:
equal(beg,end,cmpBeg);
equal(beg,end,cmpBeg,op2);
搜寻第一处不同点: // 返回pair对
mismatch(beg,end,cmpBeg);
mismatch(beg,end,cmpBeg,op2);
检验小于:
lexicographical_compare(beg1,end1,beg2,end2);
lexicographical_compare(beg1,end1,beg2,end2,op2);
变动性算法
复制元素:// 没有copy_if算法
copy(sourceBeg,sourceEnd,destBeg);
copy_backward(sourceBeg,sourceEnd,destEnd);
转换元素:
transform(sourceBeg,sourceEnd,destBeg,op1); //transform(coll2.rbegin(),coll2.rend(),ostream_iterator<int>(cout," "),negate<int>());
transform(source1Beg,source1End,source2Beg,destBeg,op2); // transform(coll1.begin(),coll2.end(),coll1.begin(),coll1.begin(),multiplies<int>());
互换元素内容:
swap_ranges(beg1,end1,beg2); //返回第二区间最后一个被交换的元素的下一位置,区间不可重叠,全部交换:swap成员函数
赋予新值:
赋予完全相同的值:
fill(beg,end,value);
fill_n(beg,n,value);
赋予新产生的数值:
generate(beg,end,op0) // generate_n(coll.begin(),coll.end(),rand);
generate(beg,n,op0) // generate_n(back_inserter(coll),5,rand);
替换元素:
替换序列中的元素:
replace(beg,end,oldValue,newValue);
replace_if(beg,end,op1,newValue);
复制并替换元素:
replace_copy(sourceBeg,sourceEnd,destBeg,oldValue,newValue);
replace_copy_if(courceBeg,sourceEnd,destBeg,op1,newValue); // (coll.begin(),coll.end(),ostream_iterator<int>(cout," "),bind2nd(less<int>(),5),42);
移除性算法 // 返回逻辑终点,不可用于关联式容器
移除序列中的某个元素:
remove(beg,end,value); // list::remove
remove_if(beg,end,op1); // coll.erase(remove_if(coll.begin(),coll.end(),bind2nd(less<int>,4)),coll.end);
复制并移除元素:
remove_copy(sourceBeg,sourceEnd,destBeg,value);
remove_copy_if(sourceBeg,sourceEnd,destBeg,op1);//remove_copy_if(coll1.begin(),coll1.end(),inserter(coll2,coll2.end()),bind2nd(less<int>(),4));
移除连续重复元素:
unique(beg,end) // list::unique
unique(beg,end,op2) // coll.erase(unique(coll.begin(),coll.end(),greater<int>()),coll.end());
复制过程中移除重复元素:
unique_copy(sourceBeg,sourceEnd,destBeg)
unique_copy(sourceBeg,sourceEnd,destBeg,op2)
变序性算法 // 不可用于关联式容器
逆转元素次序:
reverse(beg,end); // list::reverse()
reverse_copy(sourceBeg,sourceEnd,destBeg);
旋转序列中的元素:
rotate(beg,newBeg,end); // rotate(coll.begin(),coll.end()-2,coll.end());
复制并同时旋转元素:// 源区间和目标区间不得重叠
rotate_copy(sourceBeg,newBeg,sourceEnd,destBeg) // rotate_copy(coll.begin(),coll.find(4),coll.end(),ostream_iterator<int>(cout," "));
排列元素:
next_permutation(beg,end)
prev_permutation(beg,end)
搅乱次序:
random_shuffle(beg,end)
random_shuffle(beg,end,op1) // op(max)
将元素向前搬移:
partition(beg/,end,op1);
stable_partition(beg,end,op1); // stable_partition(coll1.begin(),coll1.end(),not1(bind2nd(modulus<int>(),2)));
排序算法
对所有元素排序:
sort(beg,end) // list::sort
sort(beg,end,op2);
stable_sort(beg,end);
stable_sort(beg,end,op2)
局部排序:
partial_sort(beg,sortEnd,end)
partial_sort(beg,sortEnd,end,op2)
partial_sort_copy(sourceBeg,sourceEnd,destBeg,destEnd)
partial_sort_copy(sourceBeg,sourceEnd,destBeg,destEnd,op2)
根据第n个元素排序:
nth_element(beg,nth,end);
nth_element(beg,nth,end,op2);
Heap 算法 // 随机存取迭代器
make_heap(beg,end)
make_heap(beg,end,op2)
push_heap(beg,end)
push_heap(beg,end,op2)
pop_heap(beg,end) // 将最大元素移到最后,剩余的组织起来,成为一个新的堆
pop_hap(beg,end,op2)
sort_heap(beg,end)
sort_heap(beg,end,op2)
已序区间算法
搜寻元素:
检查某个元素是否存在:
binary_search(beg,end,value)
binary_search(beg,end,value,op2)
检查若干值是否存在:
includes(beg,end,searchBeg,searchEnd)
includes(beg,end,searchBeg,searchEnd,op2)
搜寻第一个小于等于元素的位置:
lower_bound(beg,end,value)
lower_bound(beg,end,value,op2)
搜寻第一个大于元素的位置:
upper_bound(beg,end,value)
upper_bound(beg,end,value,op2)
搜寻与value相等的元素所形成的区间pair:
equal_range(beg,end,value)
equal_range(beg,end,op2)
合并元素:
合并两个已序集合:
merge(beg1,end1,beg2,end2,destBeg) // 不得重叠,源区间没有变化 ->
merge(beg1,end1,beg2,eng2,destBeg,,op2)
两个已序集合的并集:
set_union(beg1,end1,beg2,end2,destBeg)
set_union(beg1,end1,beg2,end2,destBeg,op2)
两个已序集合的交集:
set_intersection(beg1,end1,beg2,end2,destBeg)
set_intersection(beg1,end1,beg2,end2,destBeg)
两个已序集合的差集:
set_difference(beg1,end1,beg2,end2,destBeg) // destBeg内的元素只存在于第一源区间,不存在于第二源区间
set_difference(beg1,end1,beg2,end2,destBeg,op2)
两个已序集合的或集:// 只存在于第一区间或第二区间,不会同时存在于两区间内
set_symmetric_difference(beg1,end1,beg2,end2,destBeg)
set_symmetric_difference(beg1,end1,beg2,end2,destBeg,op2)
将连贯的两个已序区间合并:
inplace_merge(beg1,end1beg2,end2)
inplace_merge(beg1,end1beg2,end2,op2)
数值算法
对序列进行某种运算:
accumulate(beg,end,value) // accumulate(coll.begin(),coll.end(),0)
accumulate(beg,end,value,op2) // accumulate(coll.begin(),coll.end(),1,multiplies<int>());
计算两序列的内积:
inner_product(beg1,end1,beg2,value)
inner_product(beg1,end1,beg2,value,op21,op22) // inner_product(coll.begin(),coll.end(),coll.begin(),1,multiplies<int>(),plus<int>());
对每一项分别计算前n项和: // 源区间和目标区间可以相同
partial_sum(beg,end,destBeg);
partial_sum(beg,end,destBeg,op2) // 对每一项分别进行前n项op运算:a1,a1 op a2 ,a1 op a2 op a3,...
相邻元素差:// 源区间和目标区间可以相同
adjacent_difference(beg,end,destBeg)
adjacent_difference(beg,end,destBeg,op2) // a1, a2 op a1 ,a3 op a2 ,a4 op a3