×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Set in C++ STL (Standard Template Library)

C++ STL set container: Here, we are going to learn about the set container in C++ STL (Standard Template Library), how to use C++ STL to implement a set container?
Submitted by Radib Kar, on February 16, 2019

What is set?

The set is an ordered container (entries are always sorted after insertion) where insertion and deletion can be done based on each entry. It's an associative container can be used to hold only the unique elements.

Set operations

The main set operations are (basic ADT operations)...

  1. insert (T data): Insert data to set
  2. erase (cons T data): Deletes data from the set
  3. bool empty(): Checks for set to be empty
  4. find(const T data): finds whether data is present in the set or not

Where, T is the datatype (int / char / float etc..)

Set in C++ STL

The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, sets etc. Therefore, Set can be implemented with help of STL too.

To declare a stack of datatype T:

set<T> st; //basic STL declarations
e.g.: 
set<int> st; //set to hold integers only

To declare the set iterator

set<T> :: iterator it;
e.g.:
set<int>::iterator it;

C++ STL Set Functions

Functions available on STL set:

  1. insert (T data): Inserts data to set.
  2. erase (const T data): Deletes data from the set.
  3. empty(): Checks for set to be empty.
  4. find(const T data) : Finds whether data is present in the set or not.
  5. size(): Returns size of the set.
  6. clear(): Clears the whole set.
  7. lower_bound(T data): Returns an iterator to the first element that is equivalent to data or definitely does not go before the element data in the set.
  8. upper_bound(T data): Returns an iterator to the first element that is equivalent to data or definitely will not go beyond the element data in the set.
  9. emplace(T data): Inserts data only if data is unique based on elements already presented in set.

Kindly click on each function to check detailed code and implementation of each function, Below is the assemble of a total set operations mostly required.

C++ STL Set Example

#include <bits/stdc++.h>
using namespace std;

void printSet(set<int> st) {
  set<int>::iterator it;
  cout << "Set contents are:\n";
  for (it = st.begin(); it != st.end(); it++) cout << *it << " ";
  cout << endl;
}

int main() {
  cout << "Example of set STL\n";
  set<int> st;
  set<int>::iterator it;

  cout << "inserting 4\n";
  st.insert(4);
  cout << "inserting 6\n";
  st.insert(6);
  cout << "inserting 10\n";
  st.insert(10);

  printSet(st);  // printing current set

  cout << "erasing 6..\n";
  st.erase(6);  // prototype 1
  cout << "After erasing 6...\n";
  printSet(st);

  cout << "erasing first element of the set now\n";
  st.erase(st.begin());  // prototype 2
  cout << "after erasing first element of set now\n";
  printSet(st);

  if (st.empty())  // checking for empty
    cout << "Set is empty\n";
  else
    cout << "Set is not empty\n";

  st.clear();  // clearing the set

  cout << "Clearing the set\n";
  if (st.empty())
    cout << "Set is empty\n";
  else
    cout << "Set is not empty\n";

  return 0;
}

Output

Example of set STL 
inserting 4 
inserting 6 
inserting 10
Set contents are: 
4 6 10
erasing 6.. 
After erasing 6...
Set contents are: 
4 10
erasing first element of the set now
after erasing first element of set now
Set contents are: 
10
Set is not empty
Clearing the set
Set is empty

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.