小編最近在努力學習C++進階編程知識,趁著周末總結一下自己學習的STL算法部分的一些方法,和大家一起分享一下。
非修改性算法:
一、for_each 算法
原型: for_each(Iterator begin,Iterator end,Function fn)
功能:遍歷容器中的元素
用法:
vector <int> myvector,myvector2; fillValue(myvector); fillValue(myvector2); for_each(myvector.begin(), myvector.end(), printValue); cout << endl; for_each(myvector.begin(), myvector.end(), Multiple<int>(2)); for_each(myvector.begin(), myvector.end(), printValue); cout << endl; double sum = for_each(myvector.begin(), myvector.end(), SUM()); cout << sum << endl;
二、元素計數算法
原型: count(Iterator begin,Iterator end,const T& value)
count(Iterator begin,Iterator end,UnaryPredicate op)
功能:統計容器中滿足value和op的元素個數
用法:
int ct = count(myvector.begin(), myvector.end(), 4); int ctif = count_if(myvector.begin(), myvector.end(), isEven); int ctg = count_if(myvector.begin(), myvector.end(), bind2nd(greater<int>(), 2));
三、最大值最小值
原型:Iterator min_element(Iterator beg , Iterator end)
Iterator min_element(Iterator beg , Iterator end , compFunc op)
Iterator max_element(Iterator beg , Iterator end)
Iterator max_element(Iterator beg , Iterator end , compFunc op)
功能:尋找容器中的最大值和最小值,返回其地址。
用法:
int minNum = *min_element(myvector2.begin(), myvector2.end()); int maxNum = *max_element(myvector2.begin(), myvector2.end()); int minAbsNum = *min_element(myvector2.begin(), myvector2.end(), absLess); int maxAbsNum = *max_element(myvector2.begin(), myvector2.end(), absLess);
四、搜尋元素
1. find / find_if
原型:Iterator find(Iterator begin , Iterator end , const T& value)
Iterator fin_if(Iterator begin ,Iterator end , UnaryPredicate op)
功能:尋找容器中滿足value和op的元素
P S : distance(InputIterator first,InputIterator last) 計算兩個迭代器變量間的距離
用法:
vector<int>::iterator pos_find_1, pos_find_if; pos_find_1 = find(myvector2.begin(), myvector2.end(), 5); // 返回第一個值大於3的位置 pos_find_if = find_if(myvector2.begin(), myvector2.end(), bind2nd(greater<int>(),3)); cout << "first value = 5 pos :" << distance(myvector2.begin(),pos_find_1) + 1 << endl; cout << "first value > 5 pos :" << distance(myvector2.begin(),pos_find_if) + 1 << endl;
2. search_n
原型:Iterator search_n (Iterator begin, Iterator end, Size count, const T& value)
Iterator search_n (Iterator begin, Iterator end, Size count, const T& value,BinaryPredicate op)
功能:搜索前n個連續匹配的值
用法:
vector<int>::iterator pos_search_n_1,pos_search_n_2; // 搜索vector 中連續兩個值為2的起始位置 pos_search_n_1= search_n(myvector2.begin(), myvector2.end(), 2, 2); // 搜索vector 中連續值大於2的起始位置 pos_search_n_2 = search_n(myvector2.begin(), myvector2.end(),4,4,greater<int>()); if (pos_search_n_1 != myvector2.end()) { cout << "2個連續大於2的值的起始位置是: " << distance(myvector2.begin(), pos_search_n_1) + 1 << endl; } else { cout << "沒有找到符合要求的元素" << endl; } if (pos_search_n_2 != myvector2.end()) { cout << "4個連續大於4的值的起始位置是: " << distance(myvector2.begin(), pos_search_n_2) + 1 << endl; } else { cout << "沒有找到符合要求的元素" << endl; }
3. search
原型:Iterator search (Iterator1 begin, Iterator1 end, Iterator2 searchBegin, Iterator2 searchEnd)
Iterator search (Iterator1 begin, Iterator1 end,
Iterator2 searchBegin, Iterator2 searchEnd, BinaryPredicate op)
功能:搜索第一個子區間
用法:
vector<int> subVector; vector<int>::iterator pos_search_1,pos_search_2; setValue(subVector, -1, 3); pos_search_1 = search(myvector2.begin(), myvector2.end(), subVector.begin(), subVector.end()); if (pos_search_1 != myvector2.end()) { cout << "子串在原串的位置是(search):" << distance(myvector2.begin(), pos_search_1) + 1 << endl; } else { cout << "沒有搜索到子串" << endl; } bool checkEvenArr[3] = { true, false, true }; pos_search_2 = search(myvector2.begin(), myvector2.end(), checkEvenArr, checkEvenArr + 3, checkEven); if (pos_search_2 != myvector2.end()) { cout << "子串在原串的位置是(search + op):" << distance(myvector2.begin(), pos_search_2) + 1 << endl; } else { cout << "沒有搜索到子串" << endl; }
4. find_end
原型:Iterator find_end(Iterator begin,Iterator end,Iterator2 searchBegin,Iterator2,searchEnd)
Iterator find_end(Iterator begin,Iterator end,
Iterator2 searchBegin,Iterator2,searchEnd,binaryPredicate op)
功能:搜索最後一個子區間
用法:
vector<int>::iterator pos_find_end_1; pos_find_end_1 = find_end(myvector2.begin(), myvector2.end(), subVector.begin(), subVector.end()); if (pos_find_end_1 != myvector2.end()) { cout << "子串在原串的位置是(find_end):" << distance(myvector2.begin(), pos_find_end_1) + 1 << endl; } else { cout << "沒有搜索到子串" << endl; }
5.find_first_of
原型:Iterator find_first_of(Iterator begin,Iterator end,Iterator2 searchBegin,Iterator2,searchEnd)
Iterator find_first_of(Iterator begin,Iterator end,
Iterator2 searchBegin,Iterator2,searchEnd,binaryPredicate op)
功能:搜索某一元素第一次出現的位置
用法:
subVector.clear(); myvector.clear(); setValue(myvector, -3, 12); setValue(myvector, -3, 6); setValue(subVector, -1, 3); vector<int>::iterator pos_find_first_of_1; pos_find_first_of_1 = find_first_of(myvector.begin(), myvector.end(), subVector.begin(), subVector.end()); if (pos_find_first_of_1 != myvector.end()) { cout << "子串在原串的位置是(find_first_of):" << distance(myvector.begin(), pos_find_first_of_1) + 1 << endl; } else { cout << "沒有搜索到子串" << endl; } vector<int>::reverse_iterator rpos; rpos = find_first_of(myvector.rbegin(), myvector.rend(), subVector.begin(), subVector.end()); cout << "原串中最後一個子串的位置是:" << distance(myvector.begin(), rpos.base()) << endl;
6. adjacent_find
原型:adjacent_find(T first,T last)
adjacent_find(T first,T last,Pred op)
功能:搜索兩個連續相等的元素
最後再說兩句:
上述總結如果有錯誤的地方歡迎大家指正。小編正在努力學習,文章也會不斷更新,敬請關注!