C++学习笔记——STL【算法:遍历算法】

STL 中的常用遍历算法

for_each

功能描述:

  • 实现遍历容器,在实际开发中是最常用遍历算法

函数原型:

  • for_each(iterator beg, iterator end, _func);

    • 遍历算法 遍历容器元素
    • beg 开始迭代器
    • end 结束迭代器
    • _func 函数或者函数对象

使用案例:遍历各种 容器

#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
#include<set>
#include<algorithm>

using namespace std;

void print_val(int value) {
	cout << value << " ";
}

int main() {
	/*遍历 单端数组 vector 集合*/
	vector<int> v{1,2,3,1,2,3};
	for_each(v.begin(), v.end(), print_val); // 1 2 3 1 2 3
	cout << endl;
	/*遍历 双端数组 queue 集合*/
	deque<int> d{8,5,7,4,8,4};
	for_each(d.begin(), d.end(),print_val); // 8 5 7 4 8 4
	cout << endl;
	/*遍历 链表list容器*/
	list<int>li;
	li.push_back(1);
	li.push_front(2);
	li.push_back(3);
	li.push_front(4);
	li.push_back(5);
	for_each(li.begin(),li.end(),print_val); // 4 2 1 3 5
	cout << endl;
	/*遍历set集合*/
	set<int> se;
	se.insert(5);
	se.insert(5);
	se.insert(2);
	se.insert(1);
	se.insert(1);
	se.insert(3);
	for_each(se.begin(), se.end(), print_val); // 1 2 3 5
	return 0;
}

transform

功能描述:

容器元素转换。转换有两种:一元转换和二元转换。

一元转换 函数原型:

对容器给定范围内的每个元素做某种一元运算后放在另一个容器里。只涉及一个参与转换运算的容器。有4个参数。

  • 前2个参数:指定要转换的容器的起止范围
  • 第3个参数:结果存放容器的起始位置
  • 第4个参数:对元素的一元运算操作
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,UnaryOperation unary_op );

应用案例:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

// 使用算法遍历容器时传入的函数(传入函数名称,不加函数调用操作符)
void print_val2(int value) {
	cout << value << " ";
}

// 使用算法遍历容器时传入的函数对象(传入类名加函数调用操作符)
class MyPrint {
public:
	void operator()(int value){
		cout << value << " ";
	}
};

// 容器元素转换时传入的函数对象(传入类名加函数调用操作符)
class MyTransform {
	// 重载函数调用操作符
public:
	int operator()(int val) {
		// 按原值直接返回相当于 copy,也可以做修改
		return val;
	}
};

class MyTransform2 {
public:
	int operator()(int val) {
		// 将值乘以 2
		return 2 * val;
	}
};

int main() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i * i -1);
	}
	for_each(v1.begin(), v1.end(), print_val2); // -1 0 3 8 15 24 35 48 63 80
	cout << endl;
	vector<int> v2;
	// 根据源容器大小扩展目标容器大小
	v2.resize(v1.size());
	transform(v1.begin(),v1.end(),v2.begin(), MyTransform());
	// 查看 v1
	for_each(v1.begin(), v1.end(), print_val2); // -1 0 3 8 15 24 35 48 63 80
	cout << endl;
	// 查看 v2
	for_each(v2.begin(), v2.end(), MyPrint()); // -1 0 3 8 15 24 35 48 63 80
	cout << endl;

	vector<int>v3;
	v3.resize(v1.size());
	transform(v1.begin(), v1.end(), v3.begin(), MyTransform2());
	for_each(v3.begin(), v3.end(), MyPrint()); // -2 0 6 16 30 48 70 96 126 160


	return 0;
}

应用案例:将字符串大小写转换

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;


int main() {
	string s = "Hello World";
	cout << s << endl; // Hello World

	/*转为大写 存入 s2*/
	string s1;
	s1.resize(s.size()); // 必须要提前给目标容器开辟空间
	transform(s.begin(), s.end(), s1.begin(), toupper);
	cout << s1 << endl; // HELLO WORLD

	/*转为小写 存入当前容器 s*/
	transform(s.begin(), s.end(), s.begin(), tolower);
	cout << s << endl; // hello world
	return 0;
}

二元转换 函数原型:

对两个容器给定范围内的每个元素做二元运算后放在另一个容器里。涉及两个参与转换运算容器。有5个参数

  • 前2个参数:指定参与转换的第1个容器的起止范围
  • 第3个参数:参与转换的第2个容器的起始位置
  • 第4个参数:存放转换结果的容器的起始位置
  • 第5个参数:对元素的二元运算操作
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );

应用案例:两个容器的元素进行算术运算,结果存入第三个容器

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

void print_v(int v) {
	cout << v << " ";
}

int main() {
	
	vector<int> v1 = {1,2,3,4,5};
	vector<int> v2 = {11,12,13,14,15};
	
	// 提前给目标容器开辟合适的空间
	vector<int> v3(5);
	// 将 v1 v2 中对应位置的元素相加后存入 v3 中 
	transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), plus<int>());
	for_each(v3.begin(), v3.end(), print_v); // 12 14 16 18 20

	cout << endl;
	/*将 v4 v5 中 对应位置的元素相乘后,存入 v6*/
	vector<int> v4 = { 1,3,5 };
	vector<int> v5 = { 2,4,6 };
	vector<int> v6(3);
	transform(v4.begin(), v4.end(), v5.begin(), v6.begin(), multiplies<int>());
	for_each(v6.begin(), v6.end(), print_v); // 2 12 30

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值