algorithm此头文件是算法库的一部分。本篇介绍(有序范围上的)二分搜索操作 和集合操作。
(有序范围上的)二分搜索操作 | |
返回指向第一个不小于 给定值的元素的迭代器 (函数模板) | |
返回指向第一个大于 给定值的元素的迭代器 (函数模板) | |
确定元素是否存在于某部分有序的范围中 (函数模板) | |
返回匹配特定键值的元素范围 (函数模板) | |
其他有序范围上的操作 | |
合并两个有序范围 (函数模板) | |
就地合并两个有序范围 (函数模板) | |
(有序范围上的)集合操作 | |
若一个序列是另一个的子序列则返回 true (函数模板) | |
计算两个集合的差集 (函数模板) | |
计算两个集合的交集 (函数模板) | |
计算两个集合的对称差 (函数模板) | |
计算两个集合的并集 (函数模板) |
示例代码:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility> // std::pair
#include <vector>
#include <array>
#include <cctype> // std::tolower
#include <string>
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
bool mygreater(int i, int j) { return (i > j); }
bool myfunction(int i, int j) { return (i < j); }
int main()
{
// lower_bound/upper_bound example
int myints[] = { 10,20,30,30,20,10,10,20 };
std::vector<int> v(myints, myints + 8); // 10 20 30 30 20 10 10 20
std::sort(v.begin(), v.end()); // 10 10 10 20 20 20 30 30
std::vector<int>::iterator low, up;
low = std::lower_bound(v.begin(), v.end(), 20); //返回指向第一个不小于 给定值的元素的迭代器
up = std::upper_bound(v.begin(), v.end(), 20); // 返回指向第一个大于 给定值的元素的迭代器 ^
std::cout << "lower_bound at position " << (low - v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
// equal_range example 返回匹配特定键值的元素范围
int myints2[] = { 10,20,30,30,20,10,10,20 };
std::vector<int> v2(myints2, myints2 + 8); // 10 20 30 30 20 10 10 20
std::pair<std::vector<int>::iterator, std::vector<int>::iterator> bounds2;
// using default comparison:
std::sort(v2.begin(), v2.end()); // 10 10 10 20 20 20 30 30
bounds2 = std::equal_range(v2.begin(), v2.end(), 20); // ^ ^
// using "mygreater" as comp:
std::sort(v2.begin(), v2.end(), mygreater); // 30 30 20 20 20 10 10 10
bounds2 = std::equal_range(v2.begin(), v2.end(), 20, mygreater); // ^ ^
std::cout << "bounds2 at positions " << (bounds2.first - v2.begin());
std::cout << " and " << (bounds2.second - v2.begin()) << '\n';
// binary_search example 确定元素是否存在于某部分有序的范围中
int myints3[] = { 1,2,3,4,5,4,3,2,1 };
std::vector<int> v3(myints3, myints3 + 9); // 1 2 3 4 5 4 3 2 1
// using default comparison:
std::sort(v3.begin(), v3.end());
std::cout << "looking for a 3... ";
if (std::binary_search(v3.begin(), v3.end(), 3))
std::cout << "found!\n"; else std::cout << "not found.\n";
// using myfunction as comp:
std::sort(v3.begin(), v3.end(), myfunction);
std::cout << "looking for a 6... ";
if (std::binary_search(v3.begin(), v3.end(), 6, myfunction))
std::cout << "found!\n"; else std::cout << "not found.\n";
// merge example 合并两个有序范围
int first[] = { 5,10,15,20,25 };
int second[] = { 50,40,30,20,10 };
std::vector<int> v4(10);
std::sort(first, first + 5);
std::sort(second, second + 5);
std::merge(first, first + 5, second, second + 5, v4.begin());
std::cout << "The resulting vector contains:";
for (std::vector<int>::iterator it = v4.begin(); it != v4.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
// inplace_merge example 就地合并两个有序范围
int first5[] = { 5,10,15,20,25 };
int second5[] = { 50,40,30,20,10 };
std::vector<int> v5(10);
std::vector<int>::iterator it5;
std::sort(first5, first5 + 5);
std::sort(second5, second5 + 5);
it5 = std::copy(first5, first5 + 5, v5.begin());
std::copy(second5, second5 + 5, it5);
std::inplace_merge(v5.begin(), v5.begin() + 5, v5.end());
std::cout << "The resulting vector contains:";
for (it5 = v5.begin(); it5 != v5.end(); ++it5)
std::cout << ' ' << *it5;
std::cout << '\n';
// includes example 若一个序列是另一个的子序列则返回
int container[] = { 5,10,15,20,25,30,35,40,45,50 };
int continent[] = { 40,30,20,10 };
std::sort(container, container + 10);
std::sort(continent, continent + 4);
// using default comparison:
if (std::includes(container, container + 10, continent, continent + 4))
std::cout << "container includes continent!\n";
// using myfunction as comp:
if (std::includes(container, container + 10, continent, continent + 4, myfunction))
std::cout << "container includes continent!\n";
// set_union example 计算两个集合的并集
int first6[] = { 5,10,15,20,25 };
int second6[] = { 50,40,30,20,10 };
std::vector<int> v6(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it6;
std::sort(first6, first6 + 5); // 5 10 15 20 25
std::sort(second6, second6 + 5); // 10 20 30 40 50
it6 = std::set_union(first6, first6 + 5, second6, second6 + 5, v6.begin());
// 5 10 15 20 25 30 40 50 0 0
v6.resize(it6 - v6.begin()); // 5 10 15 20 25 30 40 50
std::cout << "The union has " << (v6.size()) << " elements:\n";
for (it6 = v6.begin(); it6 != v6.end(); ++it6)
std::cout << ' ' << *it6;
std::cout << '\n';
// set_intersection example 计算两个集合的交集
int first7[] = { 5,10,15,20,25 };
int second7[] = { 50,40,30,20,10 };
std::vector<int> v7(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it7;
std::sort(first7, first7 + 5); // 5 10 15 20 25
std::sort(second7, second7 + 5); // 10 20 30 40 50
it7 = std::set_intersection(first7, first7 + 5, second7, second7 + 5, v7.begin());
// 10 20 0 0 0 0 0 0 0 0
v7.resize(it7 - v7.begin()); // 10 20
std::cout << "The intersection has " << (v7.size()) << " elements:\n";
for (it7 = v7.begin(); it7 != v7.end(); ++it7)
std::cout << ' ' << *it7;
std::cout << '\n';
// set_difference example 计算两个集合的差集
int first8[] = { 5,10,15,20,25 };
int second8[] = { 50,40,30,20,10 };
std::vector<int> v8(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it8;
std::sort(first8, first8 + 5); // 5 10 15 20 25
std::sort(second8, second8 + 5); // 10 20 30 40 50
it8 = std::set_difference(first8, first8 + 5, second8, second8 + 5, v8.begin());
// 5 15 25 0 0 0 0 0 0 0
v8.resize(it8 - v8.begin()); // 5 15 25
std::cout << "The difference has " << (v8.size()) << " elements:\n";
for (it8 = v8.begin(); it8 != v8.end(); ++it8)
std::cout << ' ' << *it8;
std::cout << '\n';
// set_symmetric_difference example 计算两个集合的对称差
int first9[] = { 5,10,15,20,25 };
int second9[] = { 50,40,30,20,10 };
std::vector<int> v9(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it9;
std::sort(first9, first9 + 5); // 5 10 15 20 25
std::sort(second9, second9 + 5); // 10 20 30 40 50
it9 = std::set_symmetric_difference(first9, first9 + 5, second9, second9 + 5, v9.begin());
// 5 15 25 30 40 50 0 0 0 0
v9.resize(it9 - v9.begin()); // 5 15 25 30 40 50
std::cout << "The symmetric difference has " << (v9.size()) << " elements:\n";
for (it9 = v9.begin(); it9 != v9.end(); ++it9)
std::cout << ' ' << *it9;
std::cout << '\n';
std::cout << "hello world\n";
return 0;
}
运行结果:
参考: