Category: std::set
C++ Set example and Tutorial – Part 1
In this article we will discuss how to use std::set with default sorting criteria. std::set – Introduction std::set is an associative container and header that need to be include for it is,#include<set> Advertisements Benefits and Features of std::set: It’s doesn’t allow duplicate elements i.e. it only contains unique elements. Std::set can contain element of any …
C++ std::set example and tutorial with user defined classes
In this article we will see how to use std::set with user defined classes. If you want to use std::set with user defined classes and use default sorting criteria then overload operator < in the class. Advertisements For example, Suppose our class is Message that contains three properties i.e. • Member content • Sending User …
C++ std::set example and tutorial with user defined classesRead More
c++ std::set example and tutorial with external Sorting criteria | Comparator
In this article we will see how to use external Sorting Criteria i.e. Comparator in std::set. Suppose our class is Message that contains three properties i.e. • Member content • Sending User Name • Receiving User Name Advertisements Requirement: we want to keep only single message sent by each user i.e. only one sent message …
c++ std::set example and tutorial with external Sorting criteria | ComparatorRead More
Set vs Map : How to choose a right associative container ?
In this article we will discuss how set and map are different and what factors we need to keep in mind while choosing right associative container. Let’s start from set, Set : Advertisements Set is an associative container which we need to store unique elements. It always keeps the elements in sorted order. Internally it …
Set vs Map : How to choose a right associative container ?Read More
How to Access Element by index in a Set | C++
In this article we will discuss how to access an element by index in a Set. std::set is an associative container, which internally store elements in a balanced binary search tree and it doesn’t provide random access operator []. Advertisements Therefore accessing a random element from set by index is little tricky. Accessing nth element in …
C++ : Different ways to insert elements in Set
In this article we will discuss different ways to insert an element in Set. Set provides 3 different overloaded version of insert() function to add element in set. Let’s discuss them one by one, Advertisements Inserting a Single element in Set and checking the result Set is an associative container and contains only unique elements, …
Different ways to iterate over a set in C++
In this article we will discuss different ways to iterate over a set in C++. Let’s create a set of strings i.e. Advertisements // Set of strings std::set<std::string> setOfStr = { “jjj”, “khj”, “bca”, “aaa”, “ddd” }; Now let’s iterate over it and print the contents on screen using different methods i.e. Iterating over a …
Different ways to Erase / Delete an element from a Set in C++
In this article we will discuss different ways to remove an element from Set. std::set provides 3 overloaded version of erase() member function. We will discuss them one by one. Advertisements Suppose we have a set of strings i.e. //Set Of Strings std::set<std::string> setOfStrs = {“Hi”, “Hello”, “is”, “the”, “at”, “Hi”, “is”, “from”, “that”}; Let’s …
Different ways to Erase / Delete an element from a Set in C++Read More
Erase elements from a Set while Iterating in C++ & Generic erase_if()
In this article we will discuss how to erase elements from a set while iterating over it. Erasing elements from std::set while Iterating std::set provides a member function erase() that accepts an iterator as an argument and deletes it from the set i.e. iterator erase (const_iterator position); But it makes the passed iterator invalid. From …
Erase elements from a Set while Iterating in C++ & Generic erase_if()Read More
C++ : How to check if a Set contains an element | set::find vs set::count?
In this article we will discuss how to check if a given element exists in set or not. Suppose we have a set of strings i.e. Advertisements // Set of strings std::set<std::string> setOfStrs = { “the”, “is”, “of” , “from” , “at” , “hello” , “the” }; Now we wnat to check if any given …
C++ : How to check if a Set contains an element | set::find vs set::count?Read More