C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法

变异算法就是会让施加的容器中的元素发生变化

1.copy

代码示例:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

//寻找两个整型数组中元素不相等时候的元素值
int main()
{
	int a[] = {1,2,3,4,5};
	int b[5];
	
	vector<int> v;
	//将a复制给b
	copy(a, a + 5, b);
	for (int i = 0; i < 5; i++)
	{
		cout << b[i] << endl;
	}
	cout << "-----------------" << endl;
	//将a复制给vector<int>对象v
	//copy(a,a+5,v);         报错
	//报错原因:copy有以下使用注意点
	//1.区间左闭右开
	//2.如果不是STL的标准容器,那么目标容器的容量大于或等于原容器的容量

	copy(a,a+5,back_inserter(v));
	for (int i = 0; i < 5; i++)
	{
		cout << v[i] << endl;
	}
}

C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法

2.swap

可以用于基本类、数组、基本序列容器

数组:必须是真实存在的空间,而且大小相同

序列容器:不必空间相同

代码示例:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

//寻找两个整型数组中元素不相等时候的元素值
int main()
{
	//交换两个值
	int a = 4, b = 5;
	swap(a,b);
	cout << a << "\t" << b<<endl;

	cout << "----------" << endl;
	//交换两个容器
	int c[] = { 1,3,5,7,9 };
	int d[] = { 2,4,6,8,0 };
	swap_ranges(c,c+5,d);

	copy(c,c+5,ostream_iterator<int>(cout,"\t"));
	copy(d, d + 5, ostream_iterator<int>(cout, "\t"));

	cout << "\n--------------" << endl;
	int e[] = {1,2,3,4,5};
	int f[] = {100,200,300};
	vector<int> v1(e, e + 5);
	vector<int> v2(f, f + 3);
	swap(v1,v2);
	copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, "\t"));
	cout << endl;
	copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, "\t"));
}

C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法

3.transform

将某操作应用于指定范围的每个元素

代码示例:

#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <functional>

using namespace std;

template<typename T>
class Encrypt {

};

//模板特化:对修改封闭,对扩展开放
template<>
class Encrypt<string>
{
public:
	string operator()(const string& src)
	{
		string s = src;
		int len = s.length();
		for (string::iterator iter = s.begin();iter != s.end(); iter++)
		{
			*iter = *iter + 1;
		}
		return s;
	}
};
int main()
{
	string strText;
	vector<string> v;
	ifstream in("f:\\data.txt");
	while (!in.eof())
	{
		getline(in, strText, '\n');
		v.push_back(strText);
	}

	in.close();
	transform(v.begin(),v.end(),back_inserter(v),Encrypt<string>());

	copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
}

C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法

4.replace

代码示例:

#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	//所有的2都换成了10
	int a[] = {2,3,4,5,1,2,-1};
	replace(a,a+7,2,10);
	copy(a,a+7,ostream_iterator<int>(cout,"\t"));

	cout << "\n-------------------------------------" << endl;
	//这样的写法只有a[0]换成了10
	int b[] = { 2,3,4,5,1,2,-1 };
	replace(b, b + 7, b[0], 10);
	copy(b, b + 7, ostream_iterator<int>(cout, "\t"));
}

C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法

replace的参数是:first,last,const T& v,const T& new 

在使用replace时,不要让第三个参数v为first和last之内,否则替换会有问题

5.generate_n

代码示例:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <time.h>

using namespace std;

template<typename T>
class MyRandom
{};

template<>
class MyRandom<int>
{
public:
	MyRandom()
	{
		srand(time(NULL));
	}

	int operator()()
	{
		int res = rand() % 100;
		return res;
	}
};
int main()
{
	vector<int> v(10);
	generate_n(v.begin(), 10, MyRandom<int>());
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));
}

C++模板技术和STL实战开发(12)——STL容器与算法(6)——变异算法