C++ STL 常用遍歷算法
STL的容器算法迭代器的設計理念
1) STL的容器通過類模板技術,實現數據類型和容器模型的分離
2) STL的迭代器技術實現了遍歷容器的統一方法;也為STL的算法提供了統一性奠定了基 礎
3) STL的算法,通過函數對象實現了自定義數據類型的算法運算;所以說:STL的算法也提 供了統一性。
核心思想:其實函數對象本質就是回調函數,回調函數的思想:就是任務的編寫者和任務的調用者有效解耦合。函數指針做函數參數。
4) 具體例子:transform算法的輸入,通過迭代器first和last指向的元算作為輸入;通過 result作為輸出;通過函數對象來做自定義數據類型的運算。
常用的遍歷算法
for_each()
for_each: 用指定函數依次對指定范圍內所有元素進行迭代訪問。該函數不得修改 序列中的元素。
函數定義。 For_each(begin, end, func);
template<class _InIt, class _Fn1>
inline _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Func);
return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func));
}
注意for_each的第三個參數 函數對象做函數參數,函數對象做返回值
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class CMyShow { public: CMyShow() { num = 0; } void operator()(const int &iItem) { num ++; cout << iItem; } void printCount() { cout << "num:" << num << endl; } private: int num; }; void show(const int &iItem) { cout << iItem; } void mytest() { int iArray[] = {0,1,2,3,4}; vector<int> vecInt(iArray, iArray+sizeof(iArray)/sizeof(iArray[0])); for_each(vecInt.begin(), vecInt.end(), show); // 結果打印出0 1 2 3 4 CMyShow show1 = for_each(vecInt.begin(), vecInt.end(), CMyShow()); cout << endl; show1.printCount(); //顯示對象被調用的次數 return; } int main() { mytest(); system("pause"); return 0; }
transform()
transform: 與for_each類似,遍歷所有元素,但可對容器的元素進行修改
transform()算法有兩種形式:
transform(b1, e1, b2, op)
transform(b1, e1, b2, b3, op)
template<class _InIt, class _OutIt, class _Fn1>
inline _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
transform()的作用
例如:可以一個容器的元素,通過op,變換到另一個容器中(同一個容器中) 也可以把兩個容器的元素,通過op,變換到另一個容器中
注意:
1.如果目標與源相同,transform()就和for_each()一樣。
2.如果想以某值替換符合規則的元素,應使用replace()算法
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> using namespace std; int increase(int i) { return i+1; } void mytest() { vector<int> vecIntA; vecIntA.push_back(1); vecIntA.push_back(3); vecIntA.push_back(5); vecIntA.push_back(7); vecIntA.push_back(9); transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), increase); for_each(vecIntA.begin(), vecIntA.end(), [](const int & val){ cout << val << " ";}); // 2,4,6,8,10 transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), negate<int>()); for_each(vecIntA.begin(), vecIntA.end(), [](const int & val){ cout << val << " ";}); // -2, -4, -6, -8, -10 return; } int main() { mytest(); system("pause"); return 0; }
for_each()和transform()算法比較
1)STL 算法 – 修改性算法
for_each()
copy()
copy_backward()
transform()
merge()
swap_ranges()
fill()
fill_n()
generate()
generate_n()
replace
replace_if()
replace_copy()
replace_copy_if()
2)
for_each() 速度快 不靈活
transform() 速度慢 非常靈活
//一般情況下:for_each所使用的函數對象,參數是引用,沒有返回值
void mysquare(int &num)
{
num = num * num;
}
//transform所使用的函數對象,參數一般不使用引用,而是還有返回值
int mysquare2(int num) //結果的傳出,必須是通過返回值
{
return num = num * num;
}
void main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
vector<int>v2 = v1;
for_each(v1.begin(), v1.end(), mysquare); printAA(v1);
cout << endl;
transform(v2.begin(), v2.end(), v2.begin(), mysquare2); printAA(v2);
cout << endl;
}