C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset count()

C++ multiset count()

C++ multiset count()函数用于返回容器中找到的元素数。由于 multiset 容器不包含任何重复元素,因此,如果 multiset 容器中存在值val的元素,则此函数实际上返回1,否则它将返回0。

语法

size_type count (const value_type& val) const;

参数

val : 要在 multiset 容器中搜索的值。

返回值

如果值val的元素存在于 multiset 容器中,则返回1,否则返回0。

复杂度

大小为对数。

迭代器有效性

没有更改。

数据竞争

已访问容器。
同时访问容器的元素是安全的。

异常安全性

如果引发异常,则 multiset 合中没有任何更改。

示例1

让我们看一下使用给定键值搜索元素的简单示例:
#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;
}
输出:
The key 30 is present
The key 100 is not present
在上面的示例中,count()函数检查给定值。如果该元素存在于 multiset 容器中,则它将显示消息,表明存在该元素,否则不存在。

示例2

让我们来看一个简单的例子搜索 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;
}
输出:
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.
在上面的示例中,使用count()函数在 multiset 中搜索" a"至" h"元素。

示例3

让我们看一个简单的示例来搜索 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;
}
输出:
'a' is present in the multiset 
'z' is not present in the multiset
在上面的示例中,键" a"存在于 multiset m中,因此它将是" a"的值,而键" z"不存在于 multiset 中中,因此,不存在值" z" '。

示例4

让我们看一个简单的示例:
#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;  
}
输出:
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.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4