Set operations in C Plus Plus Programming Language

Set operations in C Plus Plus Programming Language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

Set operations in C Plus Plus Programming Language.The following code shows how to create set in C++ programming language.

 

Code:

#include <iostream>
#include <set>
using namespace std;

int main()
{
    // Declare a set variable
    set < int > s;

    // Insert data to set
    s.insert(5);
    s.insert(4);
    s.insert(1);
    s.insert(2);
    s.insert(3);

    // create set iterator
    set < int >::iterator i = s.begin();

    // Print full set
    while (i != s.end()) {
        cout << *i << " ";
        ++i;
    }

    // Get size of set
    cout << endl << "Size is : " <<s.size();

    // Check if a data in set
    cout << endl <<s.count(5);

    // Check the set is empty or not
    cout << endl <<s.empty();

    // Check the maximum size of set variable
    cout << endl <<s.max_size();

    s.clear();  // Clear set
    return 0;
}

 

Output:

1 2 3 4 5 
Size is : 5
1
0
576460752303423487

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram