C++ Algorithm equal_range()30 Aug 2024 | 5 min read C++ Algorithm equal_range() function is the version of binary search. This function is used to return the lower bound and upper bound of the sub range that includes all the elements equivalent to val in the range [first, last). Where sub range is defined by two iterators, one pointing to the first element that is not less than val and another pointing to the first element greater than val.
SyntaxParameterfirst: A forward iterator pointing to the first element in the range to be searched. last: A forward iterator pointing to the past last element in the range to be searched. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order otherwise, it returns false. It follows the strict weak ordering to order the elements. val: A value of the upper bound to compare the elements in the range. Return valueIt returns two iterators, one pointing to the first element that is not less than val and another pointing to the first element greater than val. If no such element is found then it returns last. ComplexityOn average, complexity is logarithmic in the distance between first and last: performs up to 2*log2 (N) + 1 element comparisons Where N = last - first. Data racesThe object in the range [first, last) are accessed. ExceptionsThis function throws an exception if either an element comparison or an operation on iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of equal_range(): Output: Lower Bound of 3 is: 3 Upper Bound of 3 is: 4 Example 2Let's see another simple example to compare the elements using operator<: Output: B C D In the above example, operator < is used to compare the elements and returns all the elements in the range which is equal to 2. Example 3Let's see another simple example to compare the elements using comparison function: Output: B C D Example 4Let's see another simple example: Output: Here are the contents of v: 2 3 5 6 7 7 7 8 9 10 Lower bound of 3 in v = 3 Upper bound of 3 in v = 5 Lower bound of 4 in v = 5 Upper bound of 4 in v = 5 Lower bound of 5 in v = 5 Upper bound of 5 in v = 6 Lower bound of 7 in v = 7 This is the first of the three 7's, since the value before this 7 is 6. Upper bound of 7 in v = 8 Lower bound of 0 in v = 2 Upper bound of 0 in v = 2 Note that both the lower and upper bound locations of 15 are the end (one-past-the-last) vector position. Next TopicC++ Algorithm |
C++ Algorithm partition() C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
C++ Algorithm replace_copy_if() C++ Algorithm replace_copy_if() function is used to make a copy of the range [first, last) to the range beginning at result, placing those for which pred returns true by new_value. It uses predicate pred instead of operator== to compare the elements. This function examines each...
8 min read
C++ Algorithm nth_element() C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and...
6 min read
C++ Algorithm shuffle() C++ Algorithm shuffle() function reorders the elements of a range by putting them at random places, using g as uniform random number generator. Syntax template <class RandomAccessIterator, class URNG> void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g); Parameter first: A random access iterator pointing the position of...
2 min read
C++ Algorithm Function copy_if() C++ Algorithm copy_if() function is used to copy the elements of the container [first,last] into a different container starting from result for which the value of pred is true. Syntax template<class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result,UnaryPredicate pred); Parameter first: It is...
1 min read
C++ Algorithm is_sorted() C++ Algorithm is_sorted() function returns true if the elements in the range [first, last) are sorted into ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntax default (1) template <class ForwardIterator> ...
4 min read
C++ Algorithm generate_n() C++ Algorithm generate_n() function is used to assign the values which is generated by a function object to a specified number of elements in a range and returns to the one past the last assigned value position. The generator function is defined by the...
3 min read
C++ Algorithm _permutation () C++ Algorithm _permutation() function is used to reorder the elements in the range [first, last) into the lexicographically greater permutation. A permutation is specified as each of several possible ways in which a set or number of things can be ordered or...
6 min read
C++ Algorithm remove() C++ Algorithm remove() function is used to eliminate all the elements that are equal to val from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the...
5 min read
C++ Algorithm Function move () C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntax template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India