
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Insertion and Deletion in STL Set C++ program
In this tutorial, we will be discussing a program to understand the insertion and deletion in STL set in C++.
The set is a container element. The properties that make it unique is that it can only contain unique elements and they can be looped over in a sorted manner.
Example
Insertion
#include<iostream> #include<set> using namespace std; int main(){ set<int> st; //declaring iterators set<int>::iterator it = st.begin(); set<int>::iterator it1, it2; pair< set<int>::iterator,bool> ptr; //inserting a single element ptr = st.insert(20); if (ptr.second) cout << "The element was newly inserted" ; else cout << "The element was already present" ; cout << "\nThe set elements after 1st insertion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; st.insert(it, 24); cout << "\nThe set elements after 2nd insertion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; int arr[3] = { 25, 24, 26 }; st.insert(arr, arr+3); cout << "\nThe set elements after 3rd insertion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; }
Output
The element was newly inserted The set elements after 1st insertion are : 20 The set elements after 2nd insertion are : 20 24 The set elements after 3rd insertion are : 20 24 25 26
Deletion
#include<iostream> #include<set> using namespace std; int main(){ set<int> st; //declaring iterators set<int>::iterator it; set<int>::iterator it1; set<int>::iterator it2; pair< set<int>::iterator,bool> ptr; //inserting values in set for (int i=1; i<10; i++) st.insert(i*5); cout << "The set elements after insertion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; it = st.begin(); cout << endl; ++it; st.erase(it); //printing set elements after deletion cout << "The set elements after 1st deletion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; st.erase(40); cout << "\nThe set elements after 2nd deletion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; ++it; ++it; ++it; ++it; st.erase(it, st.end()); cout << "\nThe set elements after 3rd deletion are : "; for (it1 = st.begin(); it1!=st.end(); ++it1) cout << *it1 << " "; cout << endl; }
Output
The set elements after insertion are : 5 10 15 20 25 30 35 40 45 The set elements after 1st deletion are : 5 15 20 25 30 35 40 45 The set elements after 2nd deletion are : 5 15 20 25 30 35 45 The set elements after 3rd deletion are : 5 15 20
- Related Articles
- Insertion and Deletion in STL Set C++
- Insertion and Deletion in Heaps in Data Sturcture
- Circular queues-Insertion and deletion operations in C++
- Python Program to Construct a Tree & Perform Insertion, Deletion, Display
- Insertion sort using C++ STL
- C++ Program to Implement Set in STL
- set::begin() and set::end() in C++ STL
- C++ program to insert delete and find from set STL
- Java Program to add and remove elements from a set which maintains the insertion order
- Insertion Sort in Python Program
- Set cbegin() and cend() function in C++ STL
- Set crbegin() and crend() function in C++ STL
- Deletion in a Binary Tree in C++ Program
- Set get_allocator() in C++ STL
- Set insert() in C++ STL

Advertisements