TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ set emplace() Function

C++ set emplace() Function with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ set emplace()

C++ set emplace() function is used to extend the set container by inserting new elements into the container. Elements are built directly (neither copied nor moved).

The constructor of the element is called by giving the arguments args passed to this function.

Insertion takes place only if key is not present already.

Syntax

template <class.... Args>
    pair<iterator, bool> emplace (Args&&... args);    //since C++ 11

Parameter

args: The arguments forwarded to construct an element to be inserted into the set.

Return value

The emplace() function returns a bool pair that will indicate if the insertion is occurred or not and returns an iterator pointing to the newly inserted element.

Complexity

Logarithmic in the container size.

Iterator validity

No changes.

Data Races

The container is modified.

Iterating ranges in the container is not safe although concurrently accessing exiting elements is safe.

Exception Safety

If an exception is thrown, there are no changes in the container.

Example 1

Let's see the simple example to insert the elements into the set:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   
   set<char> m;

   m.emplace('a');
   m.emplace('b');
   m.emplace('c');
   m.emplace('d');
   m.emplace('e');

   cout << "Set contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< ", ";

   return 0;
}

Output:

Set contains following elements
a, b, c, d, e,

In the above example, it simply insert the element into the set m with the given key value pairs.

Example 2

Let's see a simple example to insert the element and check for the duplicate key:

#include <set>  
#include <string>  
#include <iostream>  
  
using namespace std;  
  
template <typename S> void print(const S& s) {  
    cout << s.size() << " elements: ";  
  
    for (const auto& p : s) {  
        cout << "(" << p << ") ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    set<string> s1;  
  
    auto ret = s1.emplace("ten");  
  
    if (!ret.second){  
        cout << "Emplace failed, element with value \"ten\" already exists."  
            << endl << "  The existing element is (" << *ret.first << ")"  
            << endl;  
        cout << "set not modified" << endl;  
    }  
    else{  
        cout << "set modified, now contains ";  
        print(s1);  
    }  
    cout << endl;  
  
    ret = s1.emplace("ten");  
  
    if (!ret.second){  
        cout << "Emplace failed, element with value \"ten\" already exists."  
            << endl << "  The existing element is (" << *ret.first << ")"  
            << endl;  
    }  
    else{  
        cout << "set modified, now contains ";  
        print(s1);  
    }  
    cout << endl;  
}  

Output:

  set modified, now contains 1 elements: (ten) 

Emplace failed, element with value "ten" already exists.
  The existing element is (ten)

In the above example, elements are inserted in the set and when you try to use the same key "ten" then it will display an error message that key "ten" is already exist.

Example 3

Let's see a simple example to find the sum of the inserted elements:

#include <iostream>
#include <set>
using namespace std;
 
int main()
{
    // sum variable declaration
    int sum = 0;
 
    // set declaration
    set<int> myset{};
    myset.emplace(1);
    myset.emplace(7);
    myset.emplace(4);
    myset.emplace(8);
    myset.emplace(2);
    myset.emplace(5);
    myset.emplace(3);
 
    // iterator declaration
    set<int>::iterator it;
 
    // finding sum of elements
    while (!myset.empty()) {
        it = myset.begin();
        sum = sum + *it;
        myset.erase(it);
    }
 
    // printing the sum
    cout << "Sum of elements is: "<<sum;
    return 0;
}

Output:

Sum of elements is: 30

Example 4

Let's see a simple example to insert the element:

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

int main() {

  typedef set<string> city;  
   string name;
   city fmly ;
   int n;

   cout<<"Enter the number of family members :";
   cin>>n;

   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;      // Get key
       fmly.emplace(name);
       
   }
   
      cout<<"\nTotal member of family is:"<< fmly.size();

      cout<<"\nDetails of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Enter the number of family members: 3
Enter the name of each member: 
Bob
Robin
David

Total member of family is: 3
Details of family members: 

Name 
 ________________________
Bob 
David 
Robin

In the above example, it simply inserts the elements by the user's choice.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf