TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset begin() function

C++ multiset begin() 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 begin()

C++ multiset begin() function is used to return an iterator referring to the first element of the multiset container.

Syntax

      iterator begin();                            //until C++ 11

const_iterator begin() const;                //until C++ 11

      iterator begin() noexcept;              //since C++ 11

const_iterator begin() const noexcept;  //since C++ 11

Parameter

None

Return value

It returns an iterator pointing to the first element of the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed. Neither the constant nor the non-constant versions modify the container.

Exception Safety

This function never throws exceptions.

Example 1

Let's see the simple example for begin() function:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<string> mymultiset= {"Java", "C++", "SQL","C++"};

  // show content:
  cout<<"Contents of mymultiset are: "<<endl;
  for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  return 0;
}

Output:

Contents of mymultiset are: 
C++
C++
Java
SQL

In the above example, begin() function is used to return an iterator pointing to the first element in the mymultiset multiset.

Example 2

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int main()
{
  multiset<int> c;
  c.insert(5);
  c.insert(2);
  c.insert(4);
  c.insert(2);
  c.insert(0);
  c.insert(5);

  multiset<int>::iterator i = c.begin();
  while (i != c.end())
  cout << *i++ << " ";
  cout << endl;
}

Output:

0 2 2 4 5 5

Example 3

Let's see a simple example to iterate over the multiset using while loop:

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

int main()
{
    using namespace std;
 
      multiset<string> mymultiset = { "Nikita","Deep","Priya","Suman","Aman","Nikita" };

 cout<<"Elements of mymultiset are: "<<endl;
    multiset<string>::const_iterator it; // declare an iterator

    it = mymultiset.begin(); // assign it to the start of the multiset

    while (it != mymultiset.end()) // while it hasn't reach the end
    {
cout << *it << "\n"; 

// print the value of the element it points to

++it; // and iterate to the next element
    }
 
    cout << endl;
}

Output:

Elements of mymultiset are: 
Aman
Deep
Nikita
Nikita
Priya
Suman

In the above example, begin() function is used to return an iterator pointing to the first element in the mymultiset multiset.

Example 4

Let's see a simple example:

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int>::iterator s1_Iter;    
  
   s1.insert( 1 );  
   s1.insert( 2 );  
   s1.insert( 3 ); 
   s1.insert( 3 );
  
   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is " << *s1_Iter << endl;  
  
   s1_Iter = s1.begin( );  
   s1.erase( s1_Iter );  

   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is now " << *s1_Iter << endl;  
}

Output:

The first element of s1 is 1
The first element of s1 is now 2

In the above example, begin() function is used to return an iterator pointing to the first element in the mymultiset multiset.

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