c++ 中STL的遍歷算法有哪些?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
需要引入頭文件#include<algorithm>
#include<iostream> using namespace std; #include <vector> #include <algorithm> class MyPrint { public: void operator()(int val) const{ cout << val << " "; } }; void printVector(int val) { cout << val << " "; } void test() { vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } //利用普通函數(shù) for_each(v1.begin(), v1.end(), printVector); cout << endl; //利用仿函數(shù) for_each(v1.begin(), v1.end(), MyPrint()); cout << endl; } int main() { test(); system("pause"); return 0; }
#include<iostream> using namespace std; #include <vector> #include <algorithm> class Transform { public: int operator()(int val) const{ //這里可以對(duì)val進(jìn)行一些判斷 return val; } }; class MyPrint { public: void operator()(int val) const { cout << val << " "; } }; void test() { vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } vector<int> v2; //目標(biāo)容器需要先開辟空間 v2.resize(v1.size()); transform(v1.begin(), v1.end(), v2.begin(), Transform()); for_each(v2.begin(), v2.end(), MyPrint()); cout << endl; } int main() { test(); system("pause"); return 0; }
關(guān)于c++ 中STL的遍歷算法有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
新聞名稱:c++中STL的遍歷算法有哪些-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article36/ceppsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容