#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
void show( vector <int> &a){
for(auto p = a.begin(); p != a.end(); p ++)
cout << *p << ' ';
cout << endl;
}
int main(){
vector <int> a{ 1, 1, 2, 2, 1, 1};
//1 replace( iterator first, iterator last, old key, new key)
replace( a.begin(), a.end(), 1, 2);// replace all 1 by 2
show( a);
//2 replace_if( iterator first, iterator last, 一元谓词, new key)
replace_if( a.begin(), a.end(), []( int& a){
return ( a % 2 == 0);
}, -1);// replace all even numbers by -1
show( a);
}
C++ replace replace_if
最新推荐文章于 2024-06-07 20:28:43 发布