C ++ Learning -- container search method (lower_bound, uper_bound)

Source: Internet
Author: User

This article mainly verifies the use of the container search algorithm: lower_bound, uper_bound

Verification item:

1. lower_bound and uper_bound values when key> begin

2. lower_bound and uper_boudn values when key <end

3. When key = a value in the container (not bigin or end), lower_bound and uper_boudn

4. When the key is not equal to any Key in the container, but the ower_bound and uper_boudn values are returned for the key

5. The value when the key is bigin and the key is end

Test code:

# Include "stdafx. h "# include <map >#include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {// lower_bound function usage, this function is used to return the lower bound of the keyword to be searched // upper_bound function usage. This function is used to return the upper bound map <int, string> mp of the keyword to be searched; mp [3] = "3"; mp [4] = "4"; mp [7] = "7"; mp [8] = "8"; map <int, string >:: iterator iterLowerBound5, iterUperBound5; map <int, string >:: iterator iterLowerBound7, struct; map <int, string >:: iterator iterLowerBound3, struct; map <int, string >:: iterator iterLowerBound8, callback; map <int, string >:: iterator iterLowerBound10, iterUperBound10; map <int, string >:: iterator callback, callback; iterLowerBound5 = mp. lower_bound (5); iterUperBound5 = mp. upper_bound (5); iterLowerBound7 = mp. lower_bound (7); iterUperBound7 = mp. upper_bound (7); iterLowerBound3 = mp. lower_bound (0); iterUperBound3 = mp. upper_bound (0); iterLowerBound8 = mp. lower_bound (8); iterUperBound8 = mp. upper_bound (8); iterLowerBound10 = mp. lower_bound (10); iterUperBound10 = mp. upper_bound (10); if (iterLowerBound10 = mp. end () cout <"iterUperBound10 = end" <endl; if (iterUperBound10 = mp. end () cout <"iterUperBound10 = end" <endl; iterLowerBound1 = mp. lower_bound (1); iterUperBound1 = mp. upper_bound (1); if (iterLowerBound1 = mp. end () cout <"iterUperBound1 = end" <endl; if (iterUperBound1 = mp. end () cout <"iterUperBound1 = end" <endl; if (iterLowerBound1 = mp. begin () cout <"iterUperBound1 = begin" <endl; if (iterUperBound1 = mp. begin () cout <"iterUperBound1 = begin" <endl; // iter2 = mp. upper_bound (5); string Str = iterLowerBound5-> second; cout <"lower_bound (5) =" <Str. c_str () <endl; Str = iterUperBound5-> second; cout <"upper_bound (5) =" <Str. c_str () <endl; Str = iterLowerBound7-> second; cout <"lower_bound (7) =" <Str. c_str () <endl; Str = iterUperBound7-> second; cout <"upper_bound (7) =" <Str. c_str () <endl; Str = iterLowerBound3-> second; cout <"lower_bound (0) =" <Str. c_str () <endl; Str = iterUperBound3-> second; cout <"upper_bound (0) =" <Str. c_str () <endl; Str = iterLowerBound8-> second; cout <"lower_bound (8) =" <Str. c_str () <endl; // Str = iterUperBound8-> second; if (iterUperBound8 = mp. end () cout <"upper_bound (8) = end" <Str. c_str () <endl; while (1); return 0 ;}

Print Output:

IterLowerBound10 = end

IterUperBound10 = end

IterLowerBound1 = begin

IterUperBound1 = begin

Lower_bound (5) = 7

Lower_bound (5) = 7

Lower_bound (7) = 7

Lower_bound (7) = 8

Lower_bound (0) = 3

Lower_bound (0) = 3

Lower_bound (8) = 8

Lower_bound (8) = end8

Conclusion:

When the parameter key is not within the container key range:

1. If the value is smaller than the container key uper_bound, lower_bound will return begin.

2. If the value is greater than the container key uper_bound, lower_bound will return the end

When the parameter key is within the container key range:

1. The parameter key = container key. lower_bound will return the iterator of the current key, and uper_bound will return the iterator of the next element.

2. The parameter key is not equal to the container key. In the range, loer_bound returns an iterator that is larger than the parameter key and is adjacent to the container key.

3 if the Key is equal to begin or end, the system returns begin or end.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.