文章目录
c++20
定义于头文件 < algorithm >
lower-bound — 返回指向第一个不小于给定值的元素的迭代器(>= x)
template< class ForwardIt, class T, class Compare >
constexpr ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
(C++20 起)
返回指向范围 [first, last) 中首个不小于(即大于或等于) value 的元素的迭代器,或若找不到这种元素则返回 last 。
#include<iostream>
#include <algorithm>
#include<vector>
using namespace std;
int main() {
vector<int> data={2, 3, 4, 6, 7, 8, 10};
for (int i : data) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < 12; ++i) {
auto lower = lower_bound(data.begin(), data.end(), i);
cout << i << "<=";
if (lower != data.end()) {
cout << *lower << " at index " << distance(data.begin(), lower);
} else {
cout << "not found";
}
cout << endl;
}
return 0;
}
2 3 4 6 7 8 10
0<=2 at index 0
1<=2 at index 0
2<=2 at index 0
3<=3 at index 1
4<=4 at index 2
5<=6 at index 3
6<=6 at index 3
7<=7 at index 4
8<=8 at index 5
9<=10 at index 6
10<=10 at index 6
11<=not found
upper_bound — 返回指向第一个大于给定值的元素的迭代器( > x)
template< class ForwardIt, class T, class Compare >
constexpr ForwardIt upper_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );
#include<iostream>
#include <algorithm>
#include<vector>
using namespace std;
int main() {
vector<int> data={2, 3, 4, 6, 6, 7, 8, 10};
for (int i : data) {
cout << i << " ";
}
cout << endl;
auto iter = lower_bound(data.begin(), data.end(), 6);
cout << "first num >= 6 " << endl;
cout << "num : " << *iter << " loc : " << distance(data.begin(), iter) << endl << endl;
cout << "last < 6 " << endl;
if (iter == data.begin()) {
cout << "not found" << endl;
} else {
cout << "num : " << *(iter - 1) << " loc :" << distance(data.begin(), iter) << endl << endl;
}
iter = upper_bound(data.begin(), data.end(), 6);
cout << "first num > 6" << endl;
cout << "num : " << *iter << " loc : " << distance(data.begin(), iter) << endl << endl;
cout << "last <= 6" << endl;
iter -= 1;
cout << "num: " << *iter << " loc : " << distance(data.begin(), iter) << endl << endl;
return 0;
}
2 3 4 6 6 7 8 10
first num >= 6
num : 6 loc : 3
last < 6
num : 4 loc :3
first num > 6
num : 7 loc : 5
last <= 6
num: 6 loc : 4
binary_search — 确定元素是否存在于某范围中
检查等价于 value 的元素是否出现于范围 [first, last) 中。
对于要成功的 std::binary_search ,范围 [first, last) 必须至少相对于 value 部分有序.
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<string>
using namespace std;
int main() {
vector<int> haystack{1, 3, 4, 5, 9};
vector<int> needles{1, 2, 3};
for (auto i : haystack) {
cout << i << " ";
}
cout << endl;
for (auto needle : needles) {
cout << "Searching for " << needle << endl;
if (binary_search(haystack.begin(), haystack.end(), needle)) {
cout << "Found " << needle << endl;
} else {
cout << "no dice" << endl;
}
}
return 0;
}
1 3 4 5 9
Searching for 1
Found 1
Searching for 2
no dice
Searching for 3
Found 3
equal_range — 返回匹配特定键值的元素范围
template< class ForwardIt, class T, class Compare >
constexpr std::pair<ForwardIt,ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value, Compare comp );