c++STL常用算法之拷貝和替換算法——全面總結(附案例解析)(二十五)

這裏有C++STL——全面總結詳細教程(附案例解析)(持續更新中)


目錄

常用拷貝和替換算法

copy

replace

replace_if

swap

常用算術生成算法

accumulate

fill


常用拷貝和替換算法

學習目標:

  • 掌握常用的拷貝和替換算法

算法簡介:

  • copy // 容器內指定範圍的元素拷貝到另一容器中
  • replace // 將容器內指定範圍的舊元素修改爲新元素
  • replace_if // 容器內指定範圍滿足條件的元素替換爲新元素
  • swap // 互換兩個容器的元素

 

copy

功能描述:

  • 容器內指定範圍的元素拷貝到另一容器中

函數原型:

  • copy(iterator beg, iterator end, iterator dest);
  • // 按值查找元素,找到返回指定位置迭代器,找不到返回結束迭代器位置
  • // beg 開始迭代器
  • // end 結束迭代器
  • // dest 目標起始迭代器

 

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

class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		int num;
		cin >> num;
		v.push_back(num);
	}
	sort(v.begin(), v.end());
	vector < int>v2;
	v2.resize(v.size());
	copy(v.begin(), v.end(), v2.begin());

	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:利用copy算法在拷貝時,目標容器記得提前開闢空間。

 

 

 

replace

功能描述:

  • 將容器內指定範圍的舊元素修改爲新元素

函數原型:

  • replace(iterator beg, iterator end, oldvalue, newvalue);
  • // 將區間內舊元素 替換成 新元素
  • // beg 開始迭代器
  • // end 結束迭代器
  • // oldvalue 舊元素
  • // newvalue 新元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector<int>v;
	int num;
	while (cin >> num) {
		v.push_back(num);
	}
	
	replace(v.begin(), v.end(), num, 2000);
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:replace會替換區間內滿足條件的元素。

 

 

 

replace_if

功能描述:

  • 將區間內滿足條件的元素,替換成指定元素

函數原型:

  • replace_if(iterator beg, iterator end, _pred, newvalue);
  • // 按條件替換元素,滿足條件的替換成指定元素
  • // beg 開始迭代器
  • // end 結束迭代器
  • // _pred 謂詞
  • // newvalue 替換的新元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class MyPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

class ReplaceGreater30{
public:
	bool operator()(int val){
		return val >= 30;
	}
};

void test01() {
	vector<int>v;
	int num;
	while (cin >> num) {
		v.push_back(num);
	}
	
	replace_if(v.begin(), v.end(), ReplaceGreater30(), 2000);
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:replace_if按條件查找,可以利用仿函數靈活篩選滿足的條件。

 

swap

功能描述:

  • 互換兩個容器的元素

函數原型:

  • swap(container c1, container c2);
  • // 互換兩個容器的元素
  • // c1容器1
  • // c2容器2
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class myPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector<int>v1;
	vector<int>v2;
	int num;
	while (cin >> num) {
		v1.push_back(num);
		v2.push_back(num + 1000);
	}
	
	cout << "交換前: " << endl;
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;

	cout << "交換後: " << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:swap交換容器時,注意交換的容器要同種類型。

 

 

常用算術生成算法

學習目標:

  • 掌握常用的算術生成算法

注意:

  • 算術生成算法屬於小型算法,使用時包含的頭文件爲 #include <numeric>

算法簡介:

  • accumulate // 計算容器元素累計總和
  • fill // 向容器中添加元素

accumulate

功能描述:

  • 計算區間內 容器元素累計總和

函數原型:

  • accumulate(iterator beg, iterator end, value);
  • // 計算容器元素累計總和
  • // beg 開始迭代器
  • // end 結束迭代器
  • // value 起始值
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;

void test01() {
	vector<int>v1;

	int num;
	while (cin >> num) {
		v1.push_back(num);
	}
	cout << accumulate(v1.begin(), v1.end(), 0) << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:accumulate使用時頭文件注意是 numeric,這個算法很實用。

 

fill

功能描述:

  • 向容器中填充指定的元素

函數原型:

  • fill(iterator beg, iterator end, value);
  • // 向容器中填充元素
  • // beg 開始迭代器
  • // end 結束迭代器
  • // value 填充的值
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;

class myPrint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector<int>v1;
	v1.resize(10);
	fill(v1.begin(), v1.end(), 100);
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
}
int main() {
	test01();

	system("pause");
	return 0;
}

總結:利用fill可以將容器區間內元素填充爲 指定的值 。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章