TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset count() function

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

C++ Multiset count() function is used to return the number of elements found in the container. Since, the multiset container does not contain any duplicate element, this function actually returns 1 if the element with value val is present in the multiset container otherwise, it returns 0.

Syntax

size_type count (const value_type& val) const;

Parameter

val: Value to be searched in the multiset container.

Return value

It returns 1 if the element with value val is present in the multiset container or 0 otherwise.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of container is safe.

Exception Safety

If an exception is thrown, there are no changes in the multiset.

Example 1

Let's see the simple example to search the element with the given key value:

#include <iostream>
#include <set>

using namespace std;
 
int main()
{
    // initialize container
    multiset<int> mp;
 
    // insert elements in random order
    mp.insert( 30 );
    mp.insert( 40 );
    mp.insert( 30 );
    mp.insert( 20);
    mp.insert( 50 );
 
    // checks if key 30 is present or not
    if (mp.count(30))
        cout << "The key 30 is present\n";
    else
        cout << "The key 30 is not present\n";
 
    // checks if key 100 is present or not
    if (mp.count(100))
        cout << "The key 100 is present\n";
    else
        cout << "The key 100 is not present\n";
 
    return 0;
}

Output:

The key 30 is present
The key 100 is not present

In the above example, count() function checks for the given value. If the element is present in the multiset container, then it will display the message that element is present otherwise, not present.

Example 2

Let's see a simple example to search for the elements of the multiset:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<char> mymultiset;
  char c;

  mymultiset = {'a', 'c', 'f'};

  for (c='a'; c<'h'; c++)
  {
    cout << c;
    if (mymultiset.count(c)>0)
      cout << " is an element of mymultiset.\n";
    else 
      cout << " is not an element of mymultiset.\n";
  }

  return 0;
}

Output:

a is an element of mymultiset.
b is not an element of mymultiset.
c is an element of mymultiset.
d is not an element of mymultiset.
e is not an element of mymultiset.
f is an element of mymultiset.
g is not an element of mymultiset.

In the above example, count() function is used to search for the 'a' to 'h' elements in the multiset.

Example 3

Let's see a simple example to search keys in the multiset:

#include <iostream>
#include <set>

using namespace std;

int main(void) {

   multiset<char> m = {'a','b','b','d'};
            
   if (m.count('a') == 1) {
       cout<< " 'a' is present in the multiset \n";
   }

   if (m.count('z') == 0) {
      cout << " 'z' is not present in the multiset" << endl;
   }

   return 0;
}

Output:

'a' is present in the multiset 
'z' is not present in the multiset

In the above example, key 'a' is present in the multiset m hence, it will be the value of 'a' and key 'z' is not present in the multiset hence, there is no value of 'z'.

Example 4

Let's see a simple example:

#include <set>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    multiset<int> s1;  
    multiset<int>::size_type i;  
  
    s1.insert(1);  
    s1.insert(1);  
  
    // Keys must be unique in multiset, so duplicates are ignored  
    i = s1.count(1);  
    cout << "The number of elements in s1 with a sort key of 1 is: "  
         << i << "." << endl;  
  
    i = s1.count(2);  
    cout << "The number of elements in s1 with a sort key of 2 is: "  
         << i << "." << endl;  
}

Output:

The number of elements in s1 with a sort key of 1 is: 1.
The number of elements in s1 with a sort key of 2 is: 0.
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