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:
[Cpp]
# 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 of the keyword to be searched.
Map <int, string> mp;
 
Mp [3] = "3 ";
Mp [4] = "4 ";
Mp [7] = "7 ";
Mp [8] = "8 ";
 
Map <int, string >:: iterator iterLowerBound5, iterUperBound5;
Map <int, string >:: iterator iterLowerBound7, iterUperBound7;
Map <int, string >:: iterator iterLowerBound3, iterUperBound3;
Map <int, string >:: iterator iterLowerBound8, iterUperBound8;
Map <int, string >:: iterator iterLowerBound10, iterUperBound10;
Map <int, string >:: iterator iterLowerBound1, iterUperBound1;
 
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.

 


From DriverMonkey's column

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.