C++ 泛型编程:排序、拷贝和替换算法

排序算法

  • sort // 排序,默认从小到大,改变原容器
  • random_shuffle // 洗牌,改变原容器
  • merge // 合并,两个容器必须是有序的,要提前准备空间
  • reverse // 反转,改变原容器

sort 案例

// sort 案例
void printVec(int val)
{
	cout << val << " ";
}

void sort_example()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout << "升序:";
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), printVec);
	cout << endl;

	cout << "降序:";
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), printVec);
	cout << endl;
}

random_shuffle 案例

// random_shuffle 案例
void random_shuffle_example()
{
	srand((unsigned int)time(nullptr));
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), printVec);
	cout << endl;
}

merge 案例

// merge 案例
void merge_example()
{
	// 两个容器必须是有序的
	vector<int> v1;
	vector<int> v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 1);
	}

	vector<int> vTarget;
	vTarget.resize(v1.size() + v2.size());  // 提前准备空间
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), vTarget.end(), printVec);
	cout << endl;
}

reverse 案例

// reverse 案例
void reverse_example()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout << "反转前:";
	for_each(v.begin(), v.end(), printVec);
	cout << endl;

	cout << "反转后:";
	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), printVec);
	cout << endl;
}

拷贝和替换算法

  • copy // 拷贝,要提前准备空间,全容器拷贝相当于 operator=
  • replace // 将区间内旧值替换成新值
  • replace_if // 按条件替换,需要传入谓词
  • swap // 两个容器互换,不需要提前准备空间

copy 案例

// copy 案例
void printVec(int val)
{
	cout << val << " ";
}

void copy_example()
{
	vector<int> v1;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

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

	for_each(v2.begin(), v2.end(), printVec);
	cout << endl;
}

replace 案例

// replace 案例
void replace_example()
{
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);
	v.push_back(20);
	v.push_back(60);
	v.push_back(70);
	v.push_back(20);
	v.push_back(90);

	cout << "替换前:";
	for_each(v.begin(), v.end(), printVec);
	cout << endl;

	cout << "替换后:";
	replace(v.begin(), v.end(), 20, 2000);
	for_each(v.begin(), v.end(), printVec);
	cout << endl;
}

replace_if 案例

// replace_if 案例
class GT30
{
public:
	bool operator()(int val)
	{
		return val > 30;
	}
};

void replace_if_example()
{
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);
	v.push_back(20);
	v.push_back(60);
	v.push_back(70);
	v.push_back(20);
	v.push_back(90);

	cout << "替换前:";
	for_each(v.begin(), v.end(), printVec);
	cout << endl;

	cout << "替换后:";
	replace_if(v.begin(), v.end(), GT30(), 3000);
	for_each(v.begin(), v.end(), printVec);
	cout << endl;
}

swap 案例

// swap 案例
void swap_example()
{
	vector<int> v1;
	vector<int> v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 100);
	}

	cout << "替换前:" << endl;
	for_each(v1.begin(), v1.end(), printVec);
	cout << endl;
	for_each(v2.begin(), v2.end(), printVec);
	cout << endl;

	cout << "替换后:" << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), printVec);
	cout << endl;
	for_each(v2.begin(), v2.end(), printVec);
	cout << endl;
}