TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset size() function

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

C++ Multiset size() function is used to find the number of elements present in the multiset container.

Syntax

Member type size_type is an unsigned integral type.

size_type size() const;               // until C++ 11
size_type size() const noexcept;    //since C++ 11

Parameter

None

Return value

The size() function returns the number of elements present in the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example to calculate the size of the multiset:

#include <set>
#include <iostream>

using namespace std;
 
int main()
{ 
    multiset<char> num {'a', 'b', 'c', 'd', 'a'}; 
    cout << "num multiset contains " << num.size() << " elements.\n";
    return 0;
}

Output:

num multiset contains 5 elements.

In the above example, multiset num contains 5 elements. Therefore size() returns 5 elements.

Example 2

Let's see a simple example to calculate initial size of multiset and size of multiset after adding elements:

#include <iostream>
#include <set>

using namespace std;

int main(void) {

   multiset<int> m;

   cout << "Initial size of multiset = " << m.size() << endl;

   m = {1,2,3,4,5,4};

     cout << "Size of multiset after inserting elements = " << m.size() << endl;

   return 0;
}

Output:

Initial size of multiset = 0
Size of multiset after inserting elements = 6

In the above example, first multiset is empty hence, size() function will return 0 and after inserting 6 elements it will return 6.

Example 3

Let's see a simple example:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset = {100,200,300,400,200};

  while (mymultiset.size())
  {
    cout << *mymultiset.begin()<< '\n';
    mymultiset.erase(mymultiset.begin());
  }

  return 0;
}

Output:

100
200
200
300
400

In the above example, It simply uses the size() function in while loop and prints the elements of multiset until the size of multiset.

Example 4

Let's see a simple example:

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

using namespace std;

int main() {

  typedef multiset<int> marksMultiset;
   
   int number;
   marksMultiset marks;

   cout<<"Enter three sets of marks: \n";
   
   for(int i =0; i<3; i++)
   {
       cin>> number;    // Get value
       marks.insert(number);   // Put them in multiset
   }
   
      cout<<"\nSize of marks multiset is:"<< marks.size();
      cout<<"\nList of Marks: \n";
      marksMultiset::iterator p;
      for(p = marks.begin(); p!=marks.end(); p++)
      {
          cout<<(*p)<<" \n ";
      }
    
   return 0;
}

Output:

Enter three sets of marks: 
340
235
340

Size of marks multiset is: 3
List of Marks: 
235 
340 
340

In the above example, the program first creates marks multiset interactively. Then it displays the total size of marks multiset and all the elements available in the 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