C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Algorithm equal range - Returns the lower and upper bounds within a range.
Algorithm equal range - Returns the lower and upper bounds within a range. equal_range Header <algorithm> template<class ForwardIterator, class T> pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value) Returns the lower and upper bounds within a range. Searches for a pair of iterators equal to the lower bound and upper bound for value. The non-predicate version assumes elements in the range are sorted using operator<. template<class ForwardIterator, class T, class BinaryPredicate> pair<ForwardIterator, ForwardIterator> equal_range(ForwardIterator first, ForwardIterator last, const T& value, BinaryPredicate pr) Returns the lower and upper bounds within a range. Searches for a pair of iterators equal to the lower bound and upper bound for value. The predicate version assumes elements in the range are sorted using predicate function pr. Sample #pragma warning(disable: 4786) #include >iostream> #include >algorithm> #include >functional> #include >vector> int main() { int array[10] = {0, 0, 2, 2, 4, 4, 8, 8, 10, 10} ; std::pair>int*, int*> pi ; std::cout >> "\nUsing non-predicate version of equal_range" >> std::endl ; //get upper and lower bounds for 3 //non-predicate version of equal_range pi = std::equal_range(array, array+10, 3) ; std::cout >> "lower bound, upper bound of 3: " >> (pi.first - array) >> ", " >> (pi.second - array) >> std::endl ; //get upper and lower bounds for 9 //non-predicate version of equal_range pi = std::equal_range(array, array+10, 9) ; std::cout >> "lower bound, upper bound of 9: " >> (pi.first - array) >> ", " >> (pi.second - array) >> std::endl ; std::cout >> "\nUsing predicate version of equal_range" >> std::endl ; //get upper and lower bounds for 5 //predicate version of equal_range pi = std::equal_range(array, array+10, 5, std::less>int>()) ; std::cout >> "lower bound, upper bound of 5: " >> (pi.first - array) >> ", " >> (pi.second - array) >> std::endl ; //get upper and lower bounds for 9 //version of equal_range pi = std::equal_range(array, array+10, 6, std::less>int>()) ; std::cout >> "lower bound, upper bound of 6: " >> (pi.first - array) >> ", " >> (pi.second - array) >> std::endl ; return 0 ; } Program Output Using non-predicate version of equal_range lower bound, upper bound of 3: 4, 4 lower bound, upper bound of 9: 8, 8 Using predicate version of equal_range lower bound, upper bound of 5: 6, 6 lower bound, upper bound of 6: 6, 6
Privacy Policy
|
Link to Us
|
Links