欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++ STL常用算法

程序员文章站 2022-03-05 20:30:31
...

1 容器遍历:for_each

  原型:

for_each(iterator begin, iterator end, _func):
		iterator begin:开始迭代器
		iterator end:结束迭代器
		_func:函数或函数对象

  1)传递函数遍历:

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

void my_print(int val);

int main()
{
	std::vector<int> v;
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 10; i++)
	{
		v.push_back(rand() % 10);
	}
	std::for_each(v.begin(), v.end(), my_print);

}

void my_print(int val)
{
	std::cout << val << " ";
}

  输出如下:

0 5 5 7 0 3 8 3 6 5

  2)传递函数对象遍历:

#include<algorithm>
#include<iostream>
#include<map>

class MyPrint
{
public:
	void operator()(std::pair<int, int>);
};

int main()
{
	std::map<int, int> m;
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 10; i++)
	{
		m.insert(std::make_pair(i, rand() % 10));
	}
	std::for_each(m.begin(), m.end(), MyPrint());
}

void MyPrint::operator()(std::pair<int, int> p)
{
	std::cout << "姓名:" << p.first << ";年龄:" << p.second << std::endl;
}

  输出如下:

姓名:0;年龄:3
姓名:1;年龄:3
姓名:2;年龄:1
姓名:3;年龄:4
姓名:4;年龄:7
姓名:5;年龄:0
姓名:6;年龄:7
姓名:7;年龄:3
姓名:8;年龄:2
姓名:9;年龄:9

2 容器转移:transform

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

class MyPrint
{
public:
	void operator()(int);
};

class MyTransform
{
public:
	int operator()(int);
};

int main()
{
	/*
	原型:
		transform(iterator begin1, iterator end1, iterator begin2, _func):
			begin1:源数据起始索引
			end1:源数据结束索引
			begin2:目标数据起始索引
			_func:函数或者函数对象
	*/
	std::vector<int> v;
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 3; i++)
	{
		v.push_back(rand() % 10);
	}
	std::cout << "转移前:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "转移后:" << std::endl;
	std::vector<int> v_trans;
	// 必须开辟空间
	v_trans.resize(v.size());
	std::transform(v.begin(), v.end(), v_trans.begin(), MyTransform());
	std::for_each(v_trans.begin(), v_trans.end(), MyPrint());
}

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

int MyTransform::operator()(int val)
{
	// 转移的同时也可以进行其他操作
	return val + 1;
}

  输出如下:

转移前:
5 2 4
转移后:
6 3 5

3 目标查找:find

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

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		find(iterator begin, iterator end, value):
			begin:起始索引
			end:结束索引
			value:查找目标
	*/
	std::vector<int> v;
	for (int i = 0; i < 3; i++)
	{
		v.push_back(i);
	}
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::vector<int>::iterator idx = std::find(v.begin(), v.end(), 1);
	std::cout << "查找结果:" << std::endl;
	std::cout << *idx << std::endl;
}

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

  输出如下:

待查找对象:
0 1 2
查找结果:
1

4 按条件查找:find_if

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

class MyFindIf
{
public:
	bool operator()(int);
};

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		find_if(iterator begin, iterator end, _Pred):
			begin:起始索引
			end:结束索引
			_Pred:函数或者谓词 (bool类型的函数对象)
	*/
	std::vector<int> v;
	for (int i = 0; i < 3; i++)
	{
		v.push_back(i);
	}
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::vector<int>::iterator idx = std::find_if(v.begin(), v.end(), MyFindIf());
	std::cout << "查找结果:" << std::endl;
	std::cout << *idx << std::endl;
}

bool MyFindIf::operator()(int val)
{
	return val > 1;
}

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

  输出如下:

待查找对象:
0 1 2
查找结果:
2

5 查找相邻元素:adjacent_find

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

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		adjacent_find(iterator begin, iterator end):
		查找相邻重复元素,并返回相邻元素第一个位置的迭代器
			begin:起始索引
			end:结束索引
	*/
	std::vector<int> v;
	for (int i = 0; i < 3; i++)
	{
		v.push_back(i);
	}
	v.push_back(2);
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::vector<int>::iterator idx = std::adjacent_find(v.begin(), v.end());
	std::cout << "查找结果:" << std::endl;
	std::cout << *idx << std::endl;
}

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

  输出如下:

待查找对象:
0 1 2 2
查找结果:
2

6 二分查找:binary_search

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

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		bool binary_search(iterator begin, iterator end):
		二分查找,且序列必须有序,查找成功返回true,反之false
			begin:起始索引
			end:结束索引
	*/
	std::vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "查找结果:" << std::endl;
	std::cout << std::binary_search(v.begin(), v.end(), 4) << std::endl;
}

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

  输出如下:

待查找对象:
0 1 2 3 4 5 6 7 8 9
查找结果:
1

7 计数:count

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

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		count(iterator begin, iterator end, val):
		统计元素出现次数
			begin:起始索引
			end:结束索引
			val:目标
	*/
	std::vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i % 3);
	}
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "查找结果:" << std::endl;
	std::cout << std::count(v.begin(), v.end(), 1) << std::endl;
}

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

  输出如下:

待查找对象:
0 1 2 0 1 2 0 1 2 0
查找结果:
3

8 按条件计数:count_if

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

class MyCountIf
{
public:
	bool operator()(int);
};

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		count_if(iterator begin, iterator end, _Pred):
		按条件统计
			begin:起始索引
			end:结束索引
			_Pred:谓词 (bool返回值的函数对象)
	*/
	std::vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	std::cout << "待查找对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "查找结果:" << std::endl;
	std::cout << std::count_if(v.begin(), v.end(), MyCountIf()) << std::endl;
}

bool MyCountIf::operator()(int val)
{
	return val % 3 == 0;
}

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

  输出如下:

待查找对象:
0 1 2 3 4 5 6 7 8 9
查找结果:
4

9 排序:sort

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

class MySort
{
public:
	bool operator()(int, int);
};

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		sort(iterator begin, iterator end, _Pred):
		排序
			begin:起始索引
			end:结束索引
			_Pred:谓词 (bool返回值的函数对象),用于改变排序规则
	*/
	std::vector<int> v;
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 10; i++)
	{
		v.push_back(rand());
	}
	std::cout << "待排序对象:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "升序排序结果:" << std::endl;
	std::sort(v.begin(), v.end());
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "降序排序结果:" << std::endl;
	std::sort(v.begin(), v.end(), MySort());
	std::for_each(v.begin(), v.end(), MyPrint());
}

bool MySort::operator()(int val1, int val2)
{
	return val1 > val2;
}

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

  输出如下:

待排序对象:
7186 17407 23069 22918 5041 752 18209 17206 14762 2083
升序排序结果:
752 2083 5041 7186 14762 17206 17407 18209 22918 23069
降序排序结果:
23069 22918 18209 17407 17206 14762 7186 5041 2083 752

10 乱序:random_shuffle

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

class MyPrint
{
public:
	void operator()(int);
};

int main()
{
	/*
	原型:
		random_shuffle(iterator begin, iterator end):
		排序
			begin:起始索引
			end:结束索引
	*/
	std::vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	std::cout << "打乱前:" << std::endl;
	std::for_each(v.begin(), v.end(), MyPrint());
	std::cout << std::endl;
	std::cout << "打乱后:" << std::endl;
	std::random_shuffle(v.begin(), v.end());
	std::for_each(v.begin(), v.end(), MyPrint());
}

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

  输出如下:

打乱前:
0 1 2 3 4 5 6 7 8 9
打乱后:
8 1 9 2 0 5 7 3 4 6

11 merge


  输出如下:


12 reverse


  输出如下:


13 copy


  输出如下:


14 replace


  输出如下:


15 replace_if


  输出如下:


16 swap


  输出如下:


17 accumulate


  输出如下:


18 fill


  输出如下:


19 set_intersection


  输出如下:


20 set_union


  输出如下:


21 set_difference


  输出如下:


相关标签: # C++ c++ 因吉