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 Check if a Vector is Empty in C++?
Next article icon

How to Check if a Set is Empty in C++?

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

In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we'll explore different approaches to check if a set is empty in C++ STL.

Check if a Set is Empty or Not in C++

To check if a std::set is empty in C++, we can use the std::set::empty() function. This function returns a boolean value indicating whether the set is empty.

C++ Program to Check if a Set is Empty

C++
// C++ Program to show how to check if a set is empty or not #include <iostream> #include <set> using namespace std;  int main() {     // Creating sets named 'mySet1' and 'mySet2'     set<int> mySet1;     set<int> mySet2 = { 1, 2 };      // Checking if mySet1 is empty     if (mySet1.empty()) {         cout << "Set1 is empty." << endl;     }     else {         cout << "Set1 is not empty." << endl;     }      // Checking if mySet1 is empty     if (mySet2.empty()) {         cout << "Set2 is empty." << endl;     }     else {         cout << "Set2 is not empty." << endl;     }      return 0; }  // This code is contributed by Susobhan Akhuli 

Output
Set1 is empty.  Set2 is not empty.  

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

The set container in C++ STL provides an size() member function that returns the size (or number of elements) of the set. We can also check is a set is empty or not using size() function in C++ because if the size is 0, then the set is empty otherwise not.


Next Article
How to Check if a Vector is Empty in C++?
author
susobhanakhuli19
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-set
  • CPP Examples
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to Check if a List is Empty in C++?
    In C++, a list is a sequence container that allows non-contiguous memory allocation and is implemented using a doubly linked list. In this article, we will learn how to check if a list is empty in C++. Example: Input: myList = {1, 2, 3}; Output: List is not empty.Check if a List is Empty in C++To ch
    2 min read
  • How to Check if a Stack is Empty in C++?
    In C++, we have a stack data structure that follows a LIFO (Last In First Out) rule of operation. In this article, we will learn how to check if a stack is empty in C++. Example:Input:myStack = {1, 2, 3 } Output:Stack is not EmptyChecking if a Stack is Empty in C++To check if a stack is empty in C++
    2 min read
  • How to Check if a String is Empty in C++?
    In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a
    2 min read
  • How to Check if a Map is Empty in C++?
    In C++, a map is an associative container that stores elements as key-value pairs and an empty map means it contains no elements. In this article, we will learn how to check if a map is empty or not in C++. Example: Input: map<int,string>mp1 = {{1, "Ram"}, {2, "Mohit"}};map<int,string> m
    2 min read
  • How to Check if a Vector is Empty in C++?
    A vector is said to be empty when there are no elements present in vector. In this article, we will learn different ways to check whether a vector is empty or not. The most efficient way to check if the vector is empty or not is by using the vector empty() function. Let’s take a look at a simple exa
    2 min read
  • How to Check if a Deque is Empty in C++?
    In C++, a deque is a container provided by the STL library that is similar to a queue. However, unlike queues, it allows insertion and deletion from both ends. In this article, we will learn how to determine whether a deque is empty or not in C++. Example: Input: myDeque = {2, 4, 6 } Output: dq1 is
    2 min read
  • How to Check if an Array is Empty in C++?
    In C++, arrays are fixed-size data structures that allow the users to store similar data in contiguous memory locations. In this article, we will learn how to check if an array is empty in C++. Example: Input:arr[]={}arr[]={1,2,3,4,5}Output:The array is emptyThe array is not emptyCheck if an Array i
    3 min read
  • How to Check If a Set Contains an Element in C++?
    In C++, the std::set container stores the unique elements in the sorted order. In this article, we will learn how to check if the set contains a given element or not. Examples Input: s = {1, 3, 5, 7, 9}, x = 5Output: Element foundExplanation: The element 5 is present in the set. Input: s = {10, 20,
    4 min read
  • How to Create a Set of Pairs in C++?
    In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme
    2 min read
  • How to Access an Element in Set in C++?
    In C++ STL (Standard Template Library), the set container represents a collection of unique, sorted elements. In this article, we will see how to an element in a set in C++ using iterators. Example Input: mySet = {1, 8, 99, 444, 521 } Output: mySet Value at Index 2: 99Access an Element in a Set in C
    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