TheDeveloperBlog.com

Home | Contact Us

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

C++ set emplace_hint() Function

C++ set emplace_hint() 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_hint()

C++ set emplace_hint() function is used to extend the set container by inserting new elements into the container using hint as a position for element. 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>
    iterator emplace_hint (const_iterator position, Args&&... args);  //since C++ 11

Parameter

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

position: Hint for the position to insert the new elements.

Return value

It returns an iterator to the newly inserted elements. If element is already exist, insertion is failed and returns iterator to the existing element.

Complexity

If position is not specified then complexity will be logarithmic in container size.

If position is given then complexity will be amortized constant.

Iterator validity

No changes.

Data Races

The container is modified.

Iterating ranges in the container is not safe although concurrently accessing exiting elements are 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<int> m = {60, 20, 30, 40};
            
   m.emplace_hint(m.end(), 50);
   m.emplace_hint(m.begin(), 10);

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

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

   return 0;
} 

Output:

Set contains following elements
10
20
30
40 
50
60

In the above example, it simply inserts the element into the set m with the given value in the given positions.

Example 2

Let's see a simple example:

#include <set>  
#include <string>  
#include <iostream>  
  
using namespace std;  
  
template <typename M> void print(const M& m) {  
    cout << m.size() << " elements: " << endl;  
  
    for (const auto& p : m) {  
        cout << p << "  " ;  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    set<string> m1;  
  
    // Emplace some test data  
    m1.emplace("Ram");  
    m1.emplace("Rakesh");  
    m1.emplace("Sunil");  
  
    cout << "set starting data: ";  
    print(m1);  
    cout << endl;  
  
    // Emplace with hint  
    // m1.end() should be the "next" element after this emplacement  
    m1.emplace_hint(m1.end(), "Deep");  
  
    cout << "set modified, now contains ";  
    print(m1);  
    cout << endl;  
} 

Output:

set starting data: 3 elements: 
Rakesh  Ram  Sunil  

set modified, now contains 4 elements: 
Deep  Rakesh  Ram  Sunil

Example 3

Let's see a simple example to insert the elements into the set with the given position:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<char> myset;
  auto it = myset.end();

  it = myset.emplace_hint(it,'b');
  myset.emplace_hint(it,'a');
  myset.emplace_hint(myset.end(),'c');

  cout << "myset contains:";
  for (auto& x: myset)
    cout << " [" << x << ']';
    cout << '\n';

  return 0;
}

Output:

myset contains: [a] [b] [c]

Example 4

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

#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_hint(fmly.begin(),name);
       
   }
   
      cout<<"\nTotal memnbers in family are:"<< 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 fmly members : 4
Enter the name of each member: 
Deep
Sonu
Ajeet
Bob

Total memnber of fmly is:4
Details of fmly members: 

Name 
 ________________________
 Ajeet 
 Bob 
 Deep 
 Sonu

In the above example, it simply inserts the elements by the user's choice in the beginning of set.






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