C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ multiset size()C++ Multiset size() function is used to find the number of elements present in the multiset container. SyntaxMember type size_type is an unsigned integral type. size_type size() const; // until C++ 11 size_type size() const noexcept; //since C++ 11 ParameterNone Return valueThe size() function returns the number of elements present in the multiset. ComplexityConstant. Iterator validityNo changes. Data RacesThe container is accessed. Concurrently accessing the elements of a multiset container is safe. Exception SafetyThis function never throws exception. Example 1Let'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 2Let'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 3Let'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 4Let'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
|