- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - crend() Function
Description
The multiset::crend() function in C++ STL returns a const reverse iterator that points to an element that is present just before the first element in the multiset container. It is just a theoretical value and does not point to any element. It does not accept any parameters.
Syntax
Following is the syntax of multiset::crend in C++11 and onwards −
const_reverse_iterator crend() const noexcept; //C++11 onwards
Return value
It returns a const reverse iterator. It points to the element just before the first element. Since it is constant, you can not modify the elements of the multiset.
Exceptions
It never throws exceptions.
Time complexity
The time complexity of multiset::crend() function is constant.
Examples of multiset::crend() Function
The following examples demonstrate the usage of multiset::crend() function in multiset −
Printing Elements of multiset in Reverse Order
In this example, we have used the crend() function with the crbegin() function to iterate the multiset elements in the reverse order using constant iterators −
#include <iostream>
#include <set>
using namespace std;
int main(){
multiset<int> nums = {13, 31, 7, 53, 67, 53};
cout << "Multiset elements in reverse order:\n";
for (auto it = nums.crbegin(); it != nums.crend(); ++it){
cout << *it << " ";
}
return 0;
}
The output of the above code is given below −
Multiset elements in reverse order: 67 53 53 31 13 7
Getting Smallest Element using crend()
To get the smallest element in the multiset, the crend() function can be used. Since, by default multiset sorts the elements in ascending order, the crend() function returns the const reverse iterator pointing just before first element. We have used --it to move pointer to first element i.e., smallest element of the multiset.
#include <iostream>
#include <set>
using namespace std;
int main(){
multiset<int> nums = {10, 30, 20, 40, 30};
cout << "Multiset elements:";
for (auto it = nums.cbegin(); it != nums.cend(); ++it)
cout << " " << *it;
auto it = nums.crend();
// Move one step to right
--it;
cout << "\nSmallest element using crend(): " << *it << endl;
return 0;
}
The output of the above code is given below −
Multiset elements: 10 20 30 30 40 Smallest element using crend(): 10
Checking if multiset is Empty using crend()
In this example, we have an empty multiset. To check if this multiset is empty or not, we compare the iterators returned using the crbegin() and crend() functions respectively. If both the iterators point to the same point, then the multiset is empty.
#include <iostream>
#include <set>
using namespace std;
int main(){
multiset<int> mSet1 = {10, 30, 20, 40, 30};
multiset<int> mSet2;
if (mSet1.crbegin() == mSet1.crend()){
cout << "Multiset1 is empty" << endl;
} else {
cout << "Multiset1 is not empty" << endl;
}
if (mSet2.crbegin() == mSet2.crend()){
cout << "Multiset2 is empty" << endl;
} else {
cout << "Multiset2 is not empty" << endl;
}
return 0;
}
The output of the above code is given below −
Multiset1 is not empty Multiset2 is empty