不变序列算法属于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
2、find_end —— 在集合或容器中查找指定子集最后一次出现的位置
#include "iostream" #include "algorithm" #include "vector" using namespace std; int main () { int ary[] = { 1,2,3,4,5,6,2,3,4,5,6 }; vector v1(ary,ary+11); vector v2(ary+2,ary+4); for ( int i = 0; i < v1.size(); i++ ) cout << v1[i] << " "; cout << endl; for ( int i = 0; i < v2.size(); i++ ) cout << v2[i] << " "; cout << endl; vector::iterator pos; pos = find_end(v1.begin(),v1.end(), // 范围(集合) v2.begin(),v2.end()); // 子区间(子集) cout << "Position of last subrange of subrange " << "(2,4) found starting with element " << distance(v1.begin(),pos) + 1 << endl; return 0; } 运行结果: // 1 2 3 4 5 6 2 3 4 5 6 // 3 4 // Position of last subrange of subrange (2,4) // found starting with element 8
3、find_first_of —— 在集合或容器中查找指定子集第一次出现的位置
#include "iostream" #include "vector" #include "list" #include "algorithm" using namespace std; int main() { vector V; list searchV; for ( int i = 1; i < 11; i++ ) { V.insert(V.end(),i % 5); //i对5取余后从容器尾部入队 } for ( int i = 2; i < 5; i++ ) { searchV.insert(searchV.end(),i); } copy(V.begin(),V.end(), ostream_iterator(cout," ")); //从头到尾地复制到输出端 cout << endl; copy(searchV.begin(),searchV.end(), ostream_iterator(cout," ")); cout << endl; // 搜索第一次匹配searchV的首元素位置 vector::iterator pos; //迭代器 pos = find_first_of (V.begin(), V.end(),// 范围(集合) searchV.begin(), // 搜索子集头 searchV.end()); // 搜索子集尾 cout << "first element of searchV in V is element " << distance(V.begin(),pos) + 1 << endl; // 搜索最后一次匹配searchV的末元素位置 vector::reverse_iterator rpos; //反向迭代器 rpos = find_first_of (V.rbegin(), V.rend(), // 范围(集合) searchV.begin(), // 搜索子集头 searchV.end()); // 搜索子集尾 cout << "last element of searchV in V is element " << distance(V.begin(),rpos.base()) << endl; return 0; } 运行结果: // 1 2 3 4 0 1 2 3 4 0 // 2 3 4 // first element of searchV in V is element 2 // last element of searchV in V is element 9
4、find_if —— 从集合或容器中依次取元素作为指定函数的参数,如果指定函数返回真则返回当前元素位置
#include "iostream" #include "algorithm" #include "string" using namespace std; //------------------------------------------------------ bool isDon (string name) // 如果name等于Don则返回真 { return name == "Don"; } //------------------------------------------------------ string names[] = { "George", "Estelle", "Don", "Mike" }; int main () { string* ptr; ptr = find_if (names, names+4, isDon); if (ptr == names+4) cout << "Don is not on the list.\n"; else cout << "Don is element " << (ptr - names) << " on the list.\n"; return 0; } 运行结果: // Don is element 2 on the list.
5、adjacent_find —— 在集合或者容器中查找值相等的相邻元素,如果找到则返回第一个元素位置
#include "algorithm" #include "iostream" using namespace std; void main() { const int ARRAY_SIZE = 8 ; int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ; int *location;//存储第一对匹配的连续元素地址 // 打印 IntArray 内容 cout << "IntArray { " ; for(int i = 0; i < ARRAY_SIZE; i++) cout << IntArray[i] << ", " ; cout << " }" << endl ; // 在区间[first, last + 1)中查找第一对匹配的连续元素 // in the range [first, last + 1) // 此版本使用运算符==进行匹配 location = adjacent_find(IntArray, IntArray + ARRAY_SIZE); //如果找到匹配元素则打印出来 if (location != IntArray + ARRAY_SIZE)// 如果匹配 // 连续元素被找到 cout << "Found adjacent pair of matching " << "elements: (" << *location << ", " << *(location + 1) << "), " << "at location " << location - IntArray << endl; else // 连续元素未找到 cout << "No adjacent pair of matching elements were" << " found" << endl; cout << endl; cout << endl; } 运行结果: // IntArray { 1, 2, 3, 4, 4, 5, 6, 7, } // Found adjacent pair of matching elements: // (4, 4), at location 3
6、count —— 在集合或者容器中查找指定内容,返回指定内容的个数
#include "isotream" #include "algorithm" using namespace std; int main () { int a[] = {2,4,5,7,3,2,4,8,2}; int N = 0; N = count (a, a + (sizeof(a) / sizeof(a[0])), //查找的起始地址 2); // 要查找的内容 cout << "Array consists " << N << " 2's" << endl; return 0; } 运行结果: // Array consists 3 2's
7、count_if —— 从集合或容器中依次取元素作为指定函数的参数,如果指定函数返回真则返回匹配元素的个数
#include "iostream" #include "algorithm" #include "functional" #include "string" #include "vector" using namespace std; //如果参数str第一个字母为s则返回真 int MatchFirstChar( const string& str) { string s("S") ; return s == str.substr(0,1) ; } //--------------------------------------------------- void main() { const int VECTOR_SIZE = 8 ; // 定义一个string vector 容器模板 typedef vector StrVector ; // 为 string verctor 容器定义一个迭代器模板 typedef StrVector::iterator StrVectIt ; StrVector NamesV(VECTOR_SIZE); //定义一个StrVector容器 StrVectIt start, end, it ; int result = 0 ; // 存储匹配的元素个数 // 初始化 NamesV 容器 NamesV[0] = "She" ; NamesV[1] = "Sells" ; NamesV[2] = "Sea" ; NamesV[3] = "Shells" ; NamesV[4] = "by" ; NamesV[5] = "the" ; NamesV[6] = "Sea" ; NamesV[7] = "Shore" ; start = NamesV.begin() ; //NamesV 容器的第一个元素 end = NamesV.end() ; //NamesV 容器的最后一个元素 //打印 NamesV 容器的所有元素 cout << "NamesV { " ; for(it = start; it != end; it++) cout << *it << " " ; cout << " }\n" << endl ; //计算区间 [first, last +1)里面首字母以s开头的元素个数 result = count_if(start, end, MatchFirstChar); // 打印字母以s开头的元素个数 cout << "Number of elements that start" << " with letter \"S\" = " << result << endl ; } 运行结果: // NamesVect { She Sells Sea Shells by the Sea Shore } // Number of elements that start with letter "S" = 6
8、mismatch —— 比较两个集合或者容器的内容,返回第一对不匹配的元素指针
#include "iostream" #include "algorithm" using namespace std; int main () { int A1[] = { 3, 1, 4, 1, 5, 9, 3 }; int A2[] = { 3, 1, 4, 2, 8, 5, 7 }; const int N = sizeof(A1) / sizeof(int); //由于插件不支持尖括号,请自行将pair后面的【】换成尖括号 pair【int*, int*】 result = mismatch(A1, A1 + N, A2); cout << "The first mismatch is in position " << result.first - A1 + 1 << endl; cout << "Values are: " << *(result.first) << ", " << *(result.second) << endl; return 0; } 运行结果: // The first mismatch is in position 4 // Values are: 1, 2
9、for_each —— 将集合或者容器内指定范围的元素作为参数传入指定函数执行
#include "iostream" #include "algorithm" using namespace std; void inch_to_cm (double in) { cout << ( in * 2.54 ) << ' '; } //------------------------------------------------------- int main () { // 一组以厘米为单位的数据值 double inches[] = { 3.5, 6.2, 1.0, 12.87, 4.35 }; // 按英寸为单位转换输出 for_each (inches, inches+5, inch_to_cm); cout << endl; return 0; } 运行结果: // 8.89 15.748 2.54 32.6898 11.049
10、for_each —— 在集合或者容器中查找另一集合或者容器,如果找到则返回匹配的位置指针,否则返回第一个的尾指针
#include "iostream" #include "algorithm" using namespace std; int source[] = { 11, 44, 33, 11, 22, 33, 11, 22, 44 }; int pattern[] = { 11, 22, 33 }; int main () { int* ptr; ptr = search (source, source+9, pattern, pattern+3); if (ptr == source+9) // if past-the- end cout << "No match found\n"; else cout << "Match at " << (ptr - source) << endl; return 0; } OUTPUT: // Match at 3
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。