C++ set

C++ set
Techiio-author
Written by Sagar RabidasJanuary 27, 2022
6 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the C++ set

C++ set

C++ sets are special packing containers for storing particular factors so as. Ordering of the factors has to be in a specific way inside the c++ set as it places a maximum of its emphasis on the key and price pairs of factors. Every detail has to be exceptional, and once placed inside the c++ set box can not be changed as factors may be handled as steady. Even though those elements are taken into consideration regularly and can't be changed nonetheless any new element in the (key and cost layout) can be inserted and retrieved without difficulty from the box.

Syntax

C++ Set is an associative container concerning other standard library components of C++ where all the elements play an important role. The Syntax flow is represented using the following template :

template < class T_ype,
class Compare = less< T_ype >,
class Alloc = allocator< T_ype >
> class set;

The template includes the following parameters:

  • T_ype: It signifies the type of element that is present in the container.
  • Compare: A class for comparison is also introduced to take two arguments of the same type and then return a boolean value after comparison. Usage of this is optional in the sense default value will be considered if it is less than the compared values.
  • Alloc: Alloc is the class that is specifically used to allocate the values to the storage class and according to the storage class allocator

Examples

Different examples are mentioned below:

Example #1

This program demonstrates the usage of begin() and ends () method as part of the C++ set, which further represents the String set, charset, and number set with beginning and end function as shown in the output.

Code:

#include <iostream>
#include <set>
using namespace std;
int main()
{
set<string> strng_set{"iphone", "android", "basic", "landline"};
set<char> chr_set{'a', 'b', 'c', 'd'};
set<int> int_set{1, 3, 4, 5};
for (auto kt=strng_set.begin(); kt != strng_set.end(); ++kt)
cout << ' ' << *kt;
for (auto kt=chr_set.begin(); kt != chr_set.end(); ++kt)
cout << ' ' << *kt;
for (auto kt=int_set.begin(); kt != int_set.end(); ++kt)
cout << ' ' << *kt;
return 0;
}

Example #2

This program demonstrates the size method as part of the C++ set, which is used for getting the size and inserting a new set after comparison as shown in the output.

Code:

#include <iostream>
#include <set>
using namespace std;
int main()
{
set<string> strng_set{"iphone", "android", "basic", "landline"};
set<char> chr_set{'a', 'b', 'c', 'd'};
set<int> int_set{1, 3, 4, 5};
for (int i = 0; i < 4; i++) {
int_set.insert('a' + i);
}
cout << "strng_set size: " << strng_set.size();
cout << endl;
cout << "strng_set size: " << chr_set.size();
return 0;
}

Example #3

This program demonstrates the set::max size function as part of the C++ set, as shown in the output.

Code:

#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> st_1, st_2;
st_1.insert(4);
st_2.insert(8);
st_1.insert(6);
st_1.insert(5);
cout << st_1.max_size() << endl;
cout << st_2.max_size();
return 0;
}

Example #4

This program demonstrates the empty method, which returns a boolean value in the form of true or false depending on the presence of an element in the set as shown in the output.

Code:

#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> empty_set{1,2,3,4,5};
if (empty_set.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}

C
C plus plus
C plus plus set
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance