不变序列算法属于C++泛型算法中的一种,不需要修改容器内容。这些算法包括adjacent_find, find, find_end, find_first_of, count, mismatch, equal, for_each, search,下面用程序实例演示每一个算法。
1、find —— 在集合或容器中查找指定元素第一次出现的位置
#include "iostream" //因为语法高亮插件不支持尖括号,故改成引号
#include "algorithm" //实际调试过程中请自行更改
int arr[] = { 11, 22, 33, 44, 55, 66, 77, 88 };
int main ()
{
int *ptr;
ptr = find(arr, arr+8, 33); // 找第一个 33
cout << "First object with value 33 found at offset "
<< (ptr-arr) << endl;
return 0;
}
运行结果:
// First object with value 33 found at offset 2



