.\" ident @(#)upper_bound.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH upper_bound 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2upper_bound\fP \ - Determines the last valid position for a value in a sorted container. .SH SYNOPSIS .br #include .br template .RE .RS 1 ForwardIterator .RE .RS 0 upper_bound(ForwardIterator first, ForwardIterator last, .RE .RS 15 const T& value); .RE .RS 0 template .RE .RS 1 ForwardIterator .RE .RS 0 upper_bound(ForwardIterator first, ForwardIterator last, .RE .RS 15 const T& value, Compare comp); .SH DESCRIPTION The upper_bound algorithm is one of a set of binary search algorithms. All of these algorithms perform binary searches on ordered containers. Each algorithm has two versions. The first version uses the less than operator (\f2operator<\fP) to perform the comparison, and assumes that the sequence has been sorted using that operator. The second version allows you to include a function object of type \f2Compare\fP, and assumes that \f2Compare\fP is the function used to sort the sequence. The function object must be a binary predicate. The upper_bound algorithm finds the last position in a container that \f2value\fP can occupy without violating the container's ordering. upper_bound's return value is the iterator for the first element in the container that is greater than \f2value\fP, or, when the comparison operator is used, the first element that does NOT satisfy the comparison function. Because the algorithm is restricted to using the less than operator or the user-defined function to perform the search, upper_bound returns an iterator \f2i\fP in the range \f2[first, last)\fP such that for any iterator \f2j\fP in the range \f2[first, i)\fP the appropriate version of the following conditions holds: \f2!(value < *j)\fP or \f2comp(value, *j) == false\fP .SH COMPLEXITY upper_bound performs at most \f2log(last - first) + 1\fP comparisons. .SH EXAMPLE .RE .RS 0 // .br // ul_bound.cpp .br // .br #include .br #include .br #include .br #include .br using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 0 typedef vector::iterator iterator; .br int d1[11] = {0,1,2,2,3,4,2,2,2,6,7}; .br .br // Set up a vector .br vector v1(d1 + 0,d1 + 11); .br .br // Try lower_bound variants .br iterator it1 = lower_bound(v1.begin(),v1.end(),3); .br // it1 = v1.begin() + 4 .br .br iterator it2 = .RE .RS 2 lower_bound(v1.begin(),v1.end(),2,less()); .RE .RS 0 // it2 = v1.begin() + 4 .br .br // Try upper_bound variants .br iterator it3 = upper_bound(v1.begin(),v1.end(),3); .br // it3 = vector + 5 .br .br iterator it4 = .br upper_bound(v1.begin(),v1.end(),2,less()); .br // it4 = v1.begin() + 5 .br cout << endl << endl .RE .RS 5 << "The upper and lower bounds of 3: ( " .br << *it1 << " , " << *it3 << " ]" << endl; .RE .RS 0 cout << endl << endl .RE .RS 5 << "The upper and lower bounds of 2: ( " .br << *it2 << " , " << *it4 << " ]" << endl; .RE .RS 0 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br The upper and lower bounds of 3: ( 3 , 4 ] .br The upper and lower bounds of 2: ( 2 , 3 ] .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO lower_bound, equal_range