multiset::upper_bound()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向刚好大于key的下一个元素。如果参数中传入的键超过了容器中的最大键,则返回的迭代器将指向一个元素,确切的说解引用后是一个index或者pos值,该数值对应容器中最后一个元素之后的位置。
* A built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k.
** If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container
multiset_name.upper_bound(key)
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> s;
// Function to insert elements
// in the multiset container
s.insert(1);
s.insert(3);
s.insert(3);
s.insert(5);
s.insert(4);
s.insert(8);
cout << "The multiset elements are:";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " ";
// when 3 is present
auto it = s.upper_bound(3);
cout << "\nThe upper bound of key 3 is ";
cout << (*it) << endl;
// when 2 is not present
// points to next greater after 2
it = s.upper_bound(2);
cout << "The upper bound of key 2 is ";
cout << (*it) << endl;
// when 8 equal the max element in multiset
it = s.upper_bound(8);
cout << "The upper bound of key 8 is ";
cout << (*it) << endl; // result is equivalent to exceeds the max element in multiset.
// when 10 exceeds the max element in multiset
it = s.upper_bound(10);
cout << "The upper bound of key 10 is ";
cout << (*it) << endl; // get a index which is just greater than max element in multiset
// the max element in multiset is 8, which index is 5, so we get a new position is 6.
return 0;
}
Output:
The multiset elements are:1 3 3 4 5 8
The upper bound of key 3 is 4
The upper bound of key 2 is 3
The upper bound of key 8 is 6
The upper bound of key 10 is 6
multiset::lower_bound()是C++ STL中的内置函数,该函数返回一个迭代器,该迭代器指向刚好等于key的一个元素。如果参数中传递的键不在容器中,则返回的迭代器将指向刚好大于key的那个元素,如果传入的key超过了容器中的最大键,则返回的迭代器将指向一个元素确切的说解引用后是一个index或者pos值,该数值对应容器中最前面一个元素之后的位置。
* A built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter.
** In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k.
*** If the key passed in the parameter exceeds the maximum value in the container, then the iterator returned point the number of elements in the container
multiset_name.lower_bound(key)
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> s;
// Function to insert elements
// in the multiset container
s.insert(2);
s.insert(3);
s.insert(3);
s.insert(2);
s.insert(6);
cout << "The multiset elements are: ";
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << " "; // order by key automaticly, from little to bigger
// when 3 is present
auto it = s.lower_bound(3);
cout << "\nThe lower bound of key 3 is ";
cout << (*it) << endl;
// when 0 is not present
// points to next element just greater after 0
it = s.lower_bound(0);
cout << "The lower bound of key 0 is ";
cout << (*it) << endl;
// when 5 is not present
// points to next element just greater after 5
it = s.lower_bound(5);
cout << "The lower bound of key 5 is ";
cout << (*it) << endl;
// when 8 exceeds the max element in multiset
it = s.lower_bound(8);
cout << "The lower bound of key 8 is ";
cout << (*it) << endl; // get a index or pos which is just greater than the max element in multiset
// the max element in multisetis 6, which pos is 4, so we get a new position is 5.
return 0;
}
Output:
The multiset elements are: 2 2 3 3 6
The lower bound of key 3 is 3
The lower bound of key 0 is 2
The lower bound of key 5 is 6
The lower bound of key 8 is 5