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,
Inserting a Single element in Set and checking the result
Set is an associative container and contains only unique elements, therefore an insertion in set can fail if
a similar element is already present in set. Keeping that in mind set provides a version of member function
insert() i.e.
pair<iterator,bool> insert (const value_type& val);
It accepts the element to be inserted and returns a pair of Iterator & bool flag.
Frequently Asked:
If insertion is successful then value of bool flag in returned pair will be true and iterator in it will
point to the newly inserted element.
Whereas, if insertion is failed because it was a duplicate element then bool flag in returned pair will be false.
Suppose we have a set of strings i.e.
std::set<std::string> setOfStrs;
Inserting an element with insert() i.e.
// A pair of set iterator and bool std::pair<std::set<std::string>::iterator, bool> result; // Insert Returns a pair of iterator and bool result = setOfStrs.insert("Hi"); // Check if element added sucessfuly if(result.second) std::cout<<"Hi"<<" - Inserted sucessfuly"<<std::endl;
Check out complete example as follows,
Best Resources to Learn C++:
#include <iostream> #include <set> #include <iterator> #include <string> void insertInSet(std::set<std::string> & setOfStrs, std::string str) { // A pair of set iterator and bool std::pair<std::set<std::string>::iterator, bool> result; // Insert Returns a pair of iterator and bool result = setOfStrs.insert(str); // Check if element added sucessfuly if(result.second) std::cout<<str<<" - Inserted sucessfuly"<<std::endl; else std::cout<<str<<" - Not Inserted sucessfuly"<<std::endl; } int main() { std::set<std::string> setOfStrs; insertInSet(setOfStrs, "Hi"); // Try to insert the duplicate element. insertInSet(setOfStrs, "Hi"); insertInSet(setOfStrs, "the"); insertInSet(setOfStrs, "is"); insertInSet(setOfStrs, "Hello"); std::cout<<"**Map Contents***"<<std::endl; for(auto elem : setOfStrs) std::cout<<elem<<std::endl; return 0; }
Output:
Hi - Inserted sucessfuly Hi - Not Inserted sucessfuly the - Inserted sucessfuly is - Inserted sucessfuly Hello - Inserted sucessfuly **Map Contents*** Hello Hi is the
Â
Inserting an Iterator Range into a Set
std::set provides an another overloaded version of insert() i.e.
template <class InputIterator> void insert (InputIterator first, InputIterator last);
It accepts a range of input iterators and one by one all the elements in set while traversing through the range( first to last-1).
Check out the example, we are inserting elements from a vector to set by giving its iterator range to insert() function i.e.
#include <iostream> #include <set> #include <iterator> #include <string> #include <vector> int main() { std::vector<std::string> vecOfStrs = {"Hi", "Hello", "is", "the", "at", "Hi", "is"}; std::set<std::string> setOfStrs; // Insert a Range in set // Range here is start and end iterators of a vector setOfStrs.insert(vecOfStrs.begin(), vecOfStrs.end()); // It will insert all the elements in vector to set, but as // set contains only unique elements, so duplicate elements will // be automatically rejected. // But there is no way to find out how many actually inserted // because it doesn't return any value. std::cout<<"**Map Contents***"<<std::endl; for(auto elem : setOfStrs) std::cout<<elem<<std::endl; return 0; }
Output:
**Map Contents*** Hello Hi at is the
It will only add unique elements in set and reject the duplicate ones. As this version of overloaded insert() function doesn’t return any value. Hence, we have no information after the call, that what elements were actually inserted and what got rejected.
Similarly, we can insert elements from other containers too by providing a range of iterators to the insert() function.
Inserting a Initializer List in Set
std::set provides an another overloaded version of insert() i.e.
void insert (initializer_list<value_type> il);
It accepts a initializer list and insert all elements in it to the set.
Check out the example, we are inserting elements from an initiazer list to the set i.e.
#include <iostream> #include <set> #include <iterator> #include <string> int main() { std::set<std::string> setOfStrs; // Insert a Initializer list in the set setOfStrs.insert({"Hi", "Hello", "is", "the", "at", "Hi", "is"}); // It will insert all the elements in initializer_list to set, but as // set contains only unique elements, so duplicate elements will // be automatically rejected. // But there is no way to find out how many actually inserted // because it doesn't return any value. std::cout<<"**Map Contents***"<<std::endl; for(auto elem : setOfStrs) std::cout<<elem<<std::endl; return 0; }
Output:
**Map Contents*** Hello Hi at is the
It will only add unique elements in set and reject the duplicate ones. As this version of overloaded insert() function doesn’t return any value.
Hence, we have no information after the call, that what elements were actually inserted and what got rejected.
Leave a Reply
You must be logged in to post a comment.