https://en.cppreference.com
https://zh.cppreference.com
它们只会通过容器的迭代器来完成相关的操作,不会使用容器相关的任何操作。它意味着这些算法无论进行什么样的操作,容器的大小不会改变。
文章目录
accumulate() 求序列和
- int ilist_res = accumulate( ilist.begin(), ilist.end(), 0, plus() );
#include <numeric>
#include <list>
#include <functional>
#include <iostream.h>
/*
* 输出为:
accumulate()
operating on values {1,2,3,4}
result with default addition: 10
result with plus<int> function object: 10
*/
int main()
{
int ia[] = {
1, 2, 3, 4 };
list<int,allocator> ilist( ia, ia+4 );
int ia_result = accumulate(&ia[0], &ia[4], 0);
int ilist_res = accumulate(
ilist.begin(), ilist.end(), 0, plus<int>() );
cout << "accumulate()\n\t"
<< "operating on values {1,2,3,4}\n\t"
<< "result with default addition: "
<< ia_result << "\n\t"
<< "result with plus<int> function object: "
<< ilist_res
<< endl;
}
binary_search() 有序序列中查找 value
- iter = adjacent_find( vec.begin(), vec.end(),TwiceOver() );
#include <algorithm>
#include <vector>
#include <iostream.h>
#include <assert.h>
class TwiceOver {
public:
bool operator() ( int val1, int val2 )
{
return val1 == val2/2 ? true : false; }
};
int main()
{
int ia[] = {
1, 4, 4, 8 };
vector< int, allocator > vec( ia, ia+4 );
int *piter;
vector< int, allocator >::iterator iter;
// piter 指向 ia[1]
piter = adjacent_find( ia, ia+4 );
assert( *piter == ia[ 1 ] );
// iter 指向 vec[2]
iter = adjacent_find( vec.begin(), vec.end(), TwiceOver() );
assert( *iter == vec[ 2 ] );
// 到达这里表示一切顺利
cout < "ok: adjacent-find() succeeded!\n";
copy() 拷贝到由 first2 标记为开始的地方
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream.h>
/* 生成:
0 1 1 3 5 8 13
将数组序列左移 1 位
1 1 3 5 8 13 13
将 vector 序列左移 2 位
1 3 5 8 13 8 13
*/
int main()
{
int ia[] = {
0, 1, 1, 3, 5, 8, 13 };
vector< int, allocator > vec( ia, ia+7 );
ostream_iterator< int > ofile( cout, " " );
cout << "original element sequence:\n";
copy( vec.begin(), vec.end(), ofile ); cout << '\n';
// 左移 1 位
copy( ia+1, ia+7, ia );
cout << "shifting array sequence left by 1:\n";
copy( ia, ia+7, ofile ); cout << '\n';
// 左移 2 位
copy( vec.begin()+2, vec.end(), vec.begin() );
cout << "shifting vector sequence left by 2:\n";
copy( vec.begin(), vec.end(), ofile ); cout << '\n';
copy_backward() 以相反的顺序被拷贝
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream.h>
class print_elements {
public:
void operator()( string elem ) {
cout << elem
<< ( _line_cnt++%8 ? " " : "\n\t" );
}
static void reset_line_cnt() {
_line_cnt = 1; }
private:
static int _line_cnt;
};
int print_elements::_line_cnt = 1;
/* 生成:
原字符本为:
The light untonsured hair grained and hued like
pale oak
copy_backward( begin+1, end-3, end ) 后的序列为:
The light untonsured hair light untonsured hair grained
and hued
*/
int main()
{
string sa[] = {
"The", "light", "untonsured", "hair",
"grained", "and", "hued", "like", "pale", "oak" };
vector< string, allocator > svec( sa, sa+10 );
cout << "original list of strings:\n\t";
for_each( svec.begin(), svec.end(), print_elements() );
cout << "\n\n";
copy_backward( svec.begin()+1, svec.end()-3, svec.end() );
print_elements::reset_line_cnt();
cout << "sequence after "
<< "copy_backward( begin+1, end-3, end ):\n";
for_each( svec.begin(), svec.end(), print_elements() );
cout << "\n";
}
count() 返回容器中与 value 相等的元素的个数
- count( InputIterator first, InputIterator last, const Type& value );
for_each() 对所有元素应用函数 func()
- for_each()依次对[first,last)范围内的所有元素应用函数 func() ,func 不能对元素执行写操作因为前两个参数都是 Input Iterator 所以不能保证支持赋值操作 如果我们希望修改元素则应该使用 transform()算法 func 可以返回值 但是该值会被忽略。
fill() fill()将 value 的拷贝赋给[first,last)范围内的所有元素
template< class ForwardIterator, class Type >
void fill( ForwardIterator first, ForwardIterator last, const Type& value );
find() 匹配
- 发现匹配时 结束搜索过程 且 find()返回指向该元素的一个 InputIterator 如果没有发现匹配 则返回 last
reverse() 反序
rotate() 旋转
rotate()把[first,middle)范围内的元素移到容器末尾 由 middle 指向的元素成为容器的第一个元素 例如 已知单词 hissboo 则以元素 b 为轴的旋转将单词变成 boohiss
search()
- 返回一个 iterator 指向在[first1,last1)范围内第一次出现子序列
[first2,last2]的位置 如果子序列未出现 则返回 last1 例