TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset insert() function

C++ multiset insert() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ multiset insert()

C++ Multiset insert() function is used for inserting new element or a range of elements in the multiset.

Syntax

single element (1)     iterator insert (const value_type& val);   //until C++ 11

with hint (2)	iterator insert (iterator position, const value_type& val);   //until C++ 11

range (3)	  template <class InputIterator>
  		   void insert (InputIterator first, InputIterator last);        //until C++ 11

single element (1)  iterator insert (const value_type& val);
                                iterator insert (value_type&& val);                            //since C++ 11
            
with hint (2)	iterator insert (const_iterator position, const value_type& val);
                          iterator insert (const_iterator position, value_type&& val); //since C++ 11

range (3)	template <class InputIterator>
                          void insert (InputIterator first, InputIterator last);            //since C++ 11

initializer list (4)       void insert (initializer_list<value_type> il);              //since C++ 11

Parameter

val: Value to insert in the multiset.

position: Hint for the position to insert element in the multiset.

first: Beginning of range to insert value.

last: End of range to insert value.

il: An initializer list.

Return value

The insert() function return an iterator pointing to the newly inserted element in the multiset.

Complexity

If a single element is inserted, complexity will be logarithmic in size.

If a hint is given and the position given is the optimal, then the complexity will be amortized constant.

Iterator validity

No changes.

Data Races

The container is modified.

Concurrently accessing the existing elements of multiset is safe, although iterating ranges in the container is not.

Exception Safety

This function does not throw exception.

Example 1

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

#include <iostream>
#include <set>

using namespace std;

int main()
{
    multiset<int> s;
 
    s.insert(1);
    s.insert(4);
    s.insert(2);
    s.insert(3);
    s.insert(3);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

The elements in multiset are: 1 2 3 3 4

In the above example, it simply inserts the element with the given key.

Example 2

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

#include <iostream>
#include <set>

using namespace std;

int main()
{ 
    multiset<int> s;
 
    // Function to insert elements
    // in the multiset container
    auto itr = s.insert(s.begin(), 1);
 
    // the time taken to insertion
    // is very less as the correct
    // position for insertion is given
    itr = s.insert(itr, 4);
    itr = s.insert(itr, 2);
    itr = s.insert(itr, 4);
    itr = s.insert(itr, 3);
 
    cout << "The elements in multiset are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

The elements in multiset are: 1 2 3 4 4 

In the above example, elements are inserted in the defined position.

Example 3

Let's see a simple example to insert the elements of one multiset to another in a given range:

#include <iostream>
# include<iostream>
# include<set>

using namespace std;

int main()
{ 
    multiset<int> s1;
 
    // Function to insert elements
    // in the multiset container
    s1.insert(1);
    s1.insert(4);
    s1.insert(2);
    s1.insert(4);
    s1.insert(3);
 
    cout << "The elements in multiset1 are: ";
    for (auto it = s1.begin(); it != s1.end(); it++)
        cout << *it << " ";
 
    multiset<int> s2;
 
    // Function to insert one multiset to another
    // all elements from where 3 is to end is
    // inserted to multiset2
    s2.insert(s1.find(3), s1.end());
 
    cout << "\nThe elements in multiset2 are: ";
    for (auto it = s2.begin(); it != s2.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

The elements in multiset1 are: 1 2 3 4 4 
The elements in multiset2 are: 3 4 4  

Example 4

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

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<string> m = {"Java", "C++", "SQL"};
   
  // Insert the elements from an initializer_list 
   m.insert({"C++", "Oracle"});

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

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

   return 0;
}

Output:

Multiset contains following elements
C++
C++
Java
Oracle
SQL

In the above example, elements are inserted from an initializer list.

Next TopicC++ multiset




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