C++ replace,replace_if,replace_copy,replace_copy_if


#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <functional>

using namespace std;

int main(){
	
	array<int,8> t1 = {3,5,7,11,13,17,19,23};
	array<int,8> t2;

	//将容器中3替换成5 
	replace(t1.begin(),t1.end(),3,5); 

	//将所有小于15的替换成5
	replace_if(t1.begin(),t1.end(),[](int i){return i<15;},5);

	//将5替换成3,结果集放于t2中
	replace_copy(t1.begin(),t1.end(),t2.begin(),5,3);
	//将5替换成3,结果集放于t2中
	replace_copy_if(t1.begin(),t1.end(),t2.begin(),bind2nd(equal_to<int>(),5),3);

	for_each(t1.begin(),t1.end(),[](int i){cout<<i<<endl;});

	for_each(t2.begin(),t2.end(),[](int i){cout<<i<<endl;});
	return 0;
}



你可能感兴趣的:(STL)