C++ Set: Standard Template Library(STL)

0
266
Sets in C++ Example | C++ Sets Program

C++ set containers are slower than the unordered_sets containers when accessing individual elements by their key. Still, the main advantage of sets is that they allow the direct iteration of the subsets based on their order of items in the container.

Set in C++

C++ set is a built-in associative container that contains unique objects because the value of the elements identifies the container. In addition, they store unique elements in a particular order.

The value of the elements in a set cannot be modified once they are put into the container, but other operations, such as the removal and insertion of the container, can take place.

The application of sets is mainly in binary search trees. In the sets, we access elements with the help of a key identified.

Definition for the container

template < class T,	
		class Compare = less<T>				
		Class Allocation=  allocator<T>
	      > class set;

Here class T set::key_value (Here T can take any data type value) 

In next line  we compare the key set::key_compare/value_compare

In last we allocate  i.e set::allocator_type

Parameters

Key (T) – Type of the element contained. The key must be substituted by any other data type, including the user-defined data type.

Container properties

1- Associative: Elements present in the containers are generally referenced by their key and not by their absolute position in the container.

2- Ordered: The container elements follow a strict order, i.e., all the elements inserted in this set are given a position in the order.

3– Set: The value of an element is also the key used to identify it.

4- Unique keys: No two items in the container can have the same keys.

5- Allocator-aware: The container uses an allocator object to meet its storage needs.

Different functions associated with sets

1- begin() – It returns an iterator to the first element present in the set.

2- end()- It returns the iterator to the last element present in the set.

3- size()– It returns the number of elements present in the set.

4- max_size()– It returns the maximum number of items a particular set can hold simultaneously.

5- empty()- It checks and returns if the set is empty or not.

6- rbegin()– It returns a reverse iterator pointing to the last element present in the container.

7- rend()– It returns a reverse iterator, which points to the theoretical element right before the first element in the set container.

8- crend() – It returns a constant iterator that points to the position just before the first element in the container.

9- crbegin()– It returns a constant iterator that points to the last element of the container.

10- cbegin() – It returns a constant iterator pointing to the first element in the container.

11- cend()– It returns a constant iterator that points to the position past the last item in the container.

12- insert(const k)– It adds a new item, ‘k’, to the set.

13-iterator insert(iterator position, const g) – It adds a new element, ‘g’, at the position pointed by the iterator.

14- erase(iterator position)– It removes the item at the position indicated by the iterator.

15- erase(const k)– It removes the value ‘k’ from the set.

16- clear() – It removes all the elements from the set.

17- find(const k)– It returns an iterator to the item ‘k’ in the set if found; otherwise, it returns an iterator to the end.

18- count(const k ) – It returns 1 or 0 based on whether the element ‘k’ is present in the set or not.

19-lower_bound(const k)– It returns an iterator to the first element equivalent to ‘k’ or will not be there before the element ‘k’ in the set.

20 – upper_bound(const k)– It returns an iterator to the first element that is equivalent to ‘k’ or definitely will go after the item ‘k’ in the set.

There are different other functions also present for defining the set.

C++ Set Program

Q1- Write the program to insert an element in a set.

#include <iostream>
#include <set>

using namespace std;

int main()
{
  set<int> set1;
  set<int>::iterator it = set1.begin();
  set<int>::iterator iterator_1, iterator_2;

  set1.insert(20);
  set1.insert(202);
  set1.insert(230);
  set1.insert(240);
  set1.insert(250);

  //printing element after insertion
  cout << "Elements after insertion = ";
  for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1)
    cout << *iterator_1 << " ";
}

See the output.

C++ Set Program

Q2- Write a program to insert five elements in a set, delete two, and print the set.

#include <iostream>
#include <set>

using namespace std;

int main()
{
  set<int> set1;
  set<int>::iterator it = set1.begin();
  set<int>::iterator iterator_1, iterator_2;

  set1.insert(20);
  set1.insert(202);
  set1.insert(230);
  set1.insert(240);
  set1.insert(250);

  //printing element after insertion
  cout << "Elements after insertion = ";
  for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1)
    cout << *iterator_1 << " ";

  //deleting the elements
  set1.erase(20);
  set1.erase(202);

  // printing set elements after deletion
  cout << "\nElement after deletion : ";
  for (iterator_1 = set1.begin(); iterator_1 != set1.end(); ++iterator_1)
    cout << *iterator_1 << " ";
}

See the output.

Sets in C++ Example

Conclusion

Sets are associative containers where each item has to be unique because its value identifies it.

The value of an item cannot be modified once added to a set, though it can be removed and add the modified value of that item.

Finally, C++ set example tutorial is over.

Recommended Posts

Multiset in C++

Stack in C++

Priority Queue in C++

Deque in C++

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.