C++ set rend()30 Aug 2024 | 4 min read C++ set rend() function is used to return an iterator to the end of the set (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container. Note:- This is a placeholder. No element exists in this location and attempting to access is undefined behavior.SyntaxParameterNone Return valueIt returns a reverse iterator to the element following the last element of the reversed container. ComplexityConstant. Iterator validityNo changes. Data RacesThe container is accessed. Neither the const nor the non-const versions modify the container. Concurrently accessing the elements of a set is safe. Exception SafetyThis function never throws exception. Example 1Let's see the simple example for rend() function: Output: Elements are : 50 40 30 20 10 In the above example, rend() function is used to return a reverse iterator to the element following the last element of the reversed container. Because set stores the elements in sorted order of keys therefore, iterating over a set will result in above order i.e. sorted order of keys. Example 2Let's see a simple example to iterate over the set in reverse order using while loop: Output: ddd ccc bbb aaa In the above example, we are using while loop to iterate over the set in reverse order. Because set stores the elements in sorted order of keys therefore, iterating over a set will result in above order i.e. sorted order of keys. Example 3Let's see a simple example: Output: The last element in the reversed set is 10. The set is: 10 20 30 . The reversed set is: 30 20 10 . After erase, the last element in the reversed set is 20. In the above example, elements of set returned in reverse order. Example 4Let's see a simple example to sort and calculate the highest marks: Output: Salary ______________________ 5000 4500 3000 2500 1000 Highest salary: 5000 In the above example, a set emp is implemented where the ID is being stored as value and salary as key. This enables us to take advantage of the auto sorting in sets and lets us to identify the ID of the element with the highest salary. Next TopicSet crbegin() Function |
C++ There are following five uses of : default constructor: This is used to construct an empty set container with zero elements. range constructor: This is used to construct a container with the contents of range [first, last). copy constructor: This is used to construct a set with a...
4 min read
C++ std operator< Operator< is a non-member overloaded function of set in C++. This function is used to check whether the first set is less than other or not. Note: Operator < sequentially compares the element of set and comparison will stop at first mismatch. Syntax template <class T, class...
4 min read
C++ set lower_bound() C++ is used to return an iterator pointing to the key in the set container which is equivalent to val passed in the parameter. If val is not present in the set container, it returns an iterator pointing to the immediate element...
5 min read
C++ set crend() C++ is used to return a constant iterator to the end of the set (not the last element but the past last element) in reverse order. This is similar to the element preceding the first element of the non-reversed container. Note:- This is a...
3 min read
C++ set empty() C++ empty() function is used to check whether the set container is empty or not. It returns true if the set container is empty (size is 0) otherwise, it returns false. Syntax bool empty() const; ...
3 min read
C++ ~ C++ is used to destroy all the elements of set container and deallocate all the storage memory allocated by the set container. Syntax ~set(); Parameter None Return value None Complexity Linear in set::size (destructors). Iterator validity All iterators, references and pointers are invalidated. Data Races The container set and all its elements are modified. Exception Safety This function...
1 min read
C++ set cbegin() C++ is used to return a constant iterator pointing to the first element of the set container. Syntax const_iterator cbegin() const noexcept; //since C++ 11 A const_iterator is an iterator that points to constant content. Parameter None Return value It returns a const_iterator pointing to the first...
3 min read
C++ set value_comp() C++ returns a comparison object. This function is used to compare two elements to check whether the key of the first one goes before the second. It takes two arguments of the same type and returns true if the first argument precedes the second...
4 min read
C++ std swap() C++ std swap(set) is a non-member function of set in C++. This is used to swap (or exchange) the contents of two sets (i.e. x and y) but both the sets must be of same type although sizes may differ. Syntax template <class T, class Compare,...
3 min read
C++ set swap() C++ swap() function is used to swap (or exchange) the content of two sets but both the sets must be of same type although sizes may differ. Syntax void swap (set& x); Parameter x: set container to exchange the contents with. Return value None Complexity Constant. Iterator validity All references, iterators and pointers referring...
4 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