replace

来源:互联网 发布:索马里海盗抢军舰知乎 编辑:程序博客网 时间:2024/06/05 10:39

Simple example

#include <vector>#include <functional>#include <iostream>#include <algorithm>using namespace std;int main(){    vector<int> v({1,2,3,4,5,4,3,2,1});    cout << "Original Vector: " << endl;    auto itr = v.begin();    while (itr!=v.end()){        cout << *itr << " ";        itr++;    }    cout << endl;    // replace 3 with 10    cout << "After replacement: " << endl;    replace(v.begin(), v.end(), 3, 10);    itr = v.begin();    while (itr!=v.end()){        cout << *itr << " ";        itr++;    }    cout << endl;    // if the number less than 5, replace it with 0    cout << "After replacement: " << endl;    replace_if(v.begin(), v.end(), [](int i){return i<5;}, 0);    itr = v.begin();    while (itr!=v.end()){        cout << *itr << " ";        itr++;    }    cout << endl;    return 0;}


0 0
原创粉丝点击