首页 > 代码库 > STL中的拷贝替换算法(so easy)

STL中的拷贝替换算法(so easy)

#include"vector"
using namespace std;
#include"string"
#include"algorithm"
#include<iostream>

void printV(vector<int > tem)
{

	for (vector<int>::iterator it = tem.begin(); it != tem.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//copy  replace replace_if  swap 

bool lowThree(int & n)
{
	return (n < 3);
}

int main()
{
	vector<int > v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);

	vector<int > v2;
	v2.push_back(1);
	v2.push_back(6);
	v2.push_back(8);

	vector <int > v3;
	v3.resize(v1.size());
	copy(v1.begin(), v1.end(), v3.begin());
	printV(v3);

	//copy  replace replace_if  swap 
	replace(v1.begin(), v1.end(),3, 8);
	printV(v1);

	replace_if(v1.begin(), v1.end(), lowThree, 8);
	printV(v1);

	swap(v1,v2);
	printV(v1);
	printV(v2);

	system("pause");

	
}

  

STL中的拷贝替换算法(so easy)