[C++] STL container - set collection container ⑧ (Find elements greater than or equal to the specified value - set#lower_bound function | Find elements less than or equal to the specified value - set#upper_bound function)






1. Find elements greater than or equal to the specified value - set#lower_bound function



1. Function prototype


The std::set collection container class in the Standard Template Library (STL, Standard Template Library) in the C++ language provides a lower_bound member function;

The lower_bound function returns an iterator object that points to the first element in the set ordered collection that is greater than or equal to the given key value. Continue to increment the iterator to access the set collection container greater than or equal to the specified element. subsequent elements;

If there is no such element in the set, that is, the minimum value in the set is greater than the given value, the returned iterator will be equal to the end() end iterator;


The std::set#lower_bound function prototype is as follows:

iterator lower_bound(const key_type& k) const;
  • Parameter analysis: The parameter type key_type is the type of the element in std::set;
  • Return value analysis: The return value is the iterator type pointing to the element in the set; the returned iterator object points to the first element in the set ordered collection that is greater than or equal to the given key value. Continue to increment the iterator to access it. set subsequent elements in the collection container that are greater than or equal to the specified element;

2. Code examples


In the following code, a set collection container is created, which contains { 1, 2, 4, 5 }four values;

    // 初始化
    set<int> mySet = {
    
     1, 2, 4, 5 };

Call the lower_bound function to get the smallest element in the set collection container that is greater than or equal to 3;

mySet.lower_bound(3);

Code example:

#include "iostream"
using namespace std;
#include "set"

int main() {
    
    

    // 初始化
    set<int> mySet = {
    
     1, 2, 4, 5 };

    // 获取大于等于 3 的元素
    auto it = mySet.lower_bound(3);

    if (it != mySet.end()) {
    
    
        cout << "找到了大于等于 3 的最小元素是 : " << *it << endl;
    }
    else {
    
    
        cout << "没有大于等于 3 的元素 " << endl;
    }


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

The smallest element found that is greater than or equal to 3 is: 4.
Please press any key to continue. . .

Insert image description here





2. Find elements less than or equal to the specified value - set#upper_bound function



1. Function prototype


The std::set collection container class in the Standard Template Library (STL, Standard Template Library) in the C++ language provides an upper_bound member function;

The upper_bound function returns an iterator object that points to the first element in the set ordered collection that is greater than the given key value. Continue to increment the iterator to access subsequent elements in the set collection container that are greater than the specified key value. element;

If there is no such element in the set, that is, the minimum value in the set is less than the given value, the returned iterator will be equal to the end() end iterator;


The std::set#upper_bound function prototype is as follows:

iterator upper_bound(const key_type& k) const;
  • Parameter analysis: The parameter type key_type is the type of the element in std::set;
  • Return value analysis: The return value is the iterator type pointing to the element in the set; the returned iterator object points to the first element in the set ordered collection that is greater than the given key value. Continue to decrement the iterator to access the set. Subsequent elements in the collection container that are greater than the specified element;

2. Code examples


Code example:

#include "iostream"
using namespace std;
#include "set"

int main() {
    
    

    // 初始化
    set<int> mySet = {
    
     1, 2, 3, 4, 5 };

    // 获取大于 3 的元素
    auto it = mySet.upper_bound(3);

    if (it != mySet.end()) {
    
    
        cout << "找到了大于 3 的最小元素是 : " << *it << endl;
    }
    else {
    
    
        cout << "没有大于 3 的元素 " << endl;
    }


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

The smallest element found that is greater than 3 is: 4
Please press any key to continue. . .

Insert image description here





3. Find the specified key value range - set#equal_range function



1. Function prototype


The std::set collection container class in the Standard Template Library (STL, Standard Template Library) in the C++ language provides an equal_range member function;

The equal_range function returns a pair of iterators, which respectively represent the range of elements in the collection that are equal to the given key value; these two iterators are placed in a pair, of type std::pair<iterator, iterator>;

The two iterator ranges returned are the front closed range and the back open range;


Since std::setthe elements in are unique,

  • If the key value exists, the iterator range contains one element;
  • If the key value does not exist, the iterator range is empty;

The equal_range function prototype is as follows:

std::pair<iterator, iterator> equal_range(const key_type& k) const;
  • Parameter analysis: The parameter type key_type is the type of the element in std::set;
  • Return value analysis: The return value of the pair<iterator, iterator> type is a pair containing two iterators, pointing to the start and end of the range respectively. Note that the start iterator is included, but the end iterator is not included. ; The two iterator ranges returned are the former closed range and the latter open range;

2. Code examples


Code example:

#include "iostream"
using namespace std;
#include "set"

int main() {
    
    

    // 初始化
    set<int> mySet = {
    
     1, 2, 3, 4, 5 };

    // 获取等于 3 的元素范围
    auto pair_3 = mySet.equal_range(3);

    if (pair_3.first != mySet.end()) {
    
    
        cout << "找到了等 3 的元素"<< endl;
    }
    else {
    
    
        cout << "没有找到了等 3 的元素" << endl;
    }


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

Elements equal to 3 found.
Please press any key to continue. . . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135311049