Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Traverse a Set with const_iterator in C++?
Next article icon

How to Traverse a Set with reverse_iterator in C++?

Last Updated : 21 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It provides reverse iterators that make it easier to traverse the set in reverse direction. In this article, we will discuss how to traverse a set with reverse_iterator in C++.

Example

Input:
input_set = {70,10,50,90,60,40,100,30,20,80Output:
100, 90, 80, 70, 60, 50, 40, 30, 20, 10

Traverse a Set with reverse_iterator in C++

Reverse iterators in C++ are used to traverse the set backward. The std::set has two member functions that provide the reverse iterators:

  • set::begin(): Return the reverse iterator pointing to the last element in the set.
  • set::rend(): Return a reverse iterator pointing to the theoretical element right before the first element in the set.

C++ Program to Traverse a Set with reverse_iterator

C++
// C++ program to traverse the set using the // reverse_iterator. #include <iostream> #include <set> using namespace std;  // Function to display elements of the set // using reverse_iterator void display(set<int> set1) {     set<int>::reverse_iterator r_itr;     for (r_itr = set1.rbegin(); r_itr != set1.rend();          r_itr++) {         cout << *r_itr << ", ";     } } // Driver Code int main() {     // Create an empty set     set<int> set1;      // Insert 10 elements into the set     set1.insert(70);     set1.insert(10);     set1.insert(50);     set1.insert(90);     set1.insert(60);     set1.insert(40);     set1.insert(100);     set1.insert(30);     set1.insert(20);     set1.insert(80);      // Call the display() function     display(set1);      return 0; } 

Output
100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 

Time Complexity: O(N)
Auxiliary Space: O(1)




Next Article
How to Traverse a Set with const_iterator in C++?
author
gottumukkala_sivanagulu
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-set
  • CPP Examples
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to Traverse a List with reverse_iterator in C++?
    In C++, lists are containers that allow users to store data in non-contiguous memory locations. List provides reverse iterators that make it easy to traverse the list in reverse order. In this article, we will learn how to traverse a list with reverse_iterator in C++. Example Input: myList = {10,20,
    2 min read
  • How to Traverse a Set with const_iterator in C++?
    In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in
    2 min read
  • How to Use a reverse_Iterator with a Map in C++?
    In C++, a reverse_iterator is a type of iterator that points to elements in a container in the reverse order. This means that using a reverse_iterator, you can traverse a container from end to beginning. In this article, we will learn how to use a reverse_iterator with a map in C++. Example: Input:
    2 min read
  • How to Traverse a List with const_iterator in C++?
    In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50}
    2 min read
  • How to Traverse Vector Using const_reverse_iterator in C++?
    In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato
    2 min read
  • How to Traverse a Map Using Iterator in C++?
    In C++, maps are associative containers that store elements where each element has a key value and a mapped value. An iterator is a pointer-like object that allows traversing through the elements of a container like a map. In this article, we will learn how to traverse a map using an iterator in C++
    2 min read
  • How to Reverse Iterate a Vector in C++?
    In this article, we will learn different methods to iterate through the vector in reverse order in C++. The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Let’s take a look at an example: [GFGTABS] C++ #include <bits/stdc++.h> using namespace
    2 min read
  • How to Use Iterator with a Vector in C++?
    In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an
    2 min read
  • How to Use const_iterator with a Map in C++?
    In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{“apple”, 1}
    2 min read
  • How to Traverse Vector using const_iterator in C++?
    In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences