इस लेख में हम C++ STL में मल्टीमैप::काउंट() फंक्शन की कार्यप्रणाली, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
C++ STL में मल्टीमैप क्या है?
मल्टीमैप सहयोगी कंटेनर हैं, जो मानचित्र कंटेनर के समान हैं। यह एक विशिष्ट क्रम में की-वैल्यू और मैप्ड वैल्यू के संयोजन से बनने वाले तत्वों को स्टोर करने की सुविधा भी देता है। एक मल्टीमैप कंटेनर में, एक ही कुंजी से जुड़े कई तत्व हो सकते हैं। डेटा को आंतरिक रूप से हमेशा संबंधित कुंजियों की सहायता से क्रमबद्ध किया जाता है।
मल्टीमैप क्या है::गिनती()?
Multimap::count() फ़ंक्शन C++ STL में एक इनबिल्ट फ़ंक्शन है, जिसे
यदि मल्टीमैप कंटेनर में कुंजी मौजूद नहीं है तो यह फ़ंक्शन शून्य लौटाता है।
सिंटैक्स
multimap_name.count(key_type& key);
पैरामीटर
फ़ंक्शन निम्नलिखित पैरामीटर स्वीकार करता है -
-
कुंजी - यह वह कुंजी है जिसे हम खोजना चाहते हैं और कुंजी से जुड़े तत्वों की संख्या की गणना करना चाहते हैं।
रिटर्न वैल्यू
यह फ़ंक्शन एक पूर्णांक यानी समान कुंजी वाले तत्वों की संख्या देता है।
इनपुट
std::multimap<char, int> odd, eve; odd.insert(make_pair(‘a’, 1)); odd.insert(make_pair(‘a, 3)); odd.insert(make_pair(‘c’, 5)); odd.count(‘a’);
आउटपुट
2
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main(){
//create the container
multimap<int, int> mul;
//insert using emplace
mul.emplace_hint(mul.begin(), 1, 10);
mul.emplace_hint(mul.begin(), 2, 20);
mul.emplace_hint(mul.begin(), 2, 30);
mul.emplace_hint(mul.begin(), 1, 40);
mul.emplace_hint(mul.begin(), 1, 50);
mul.emplace_hint(mul.begin(), 5, 60);
cout << "\nElements in multimap is : \n";
cout <<"KEY\tELEMENT\n";
for (auto i = mul.begin(); i!= mul.end(); i++){
cout << i->first << "\t" << i->second << endl;
}
cout<<"Key 1 appears " << mul.count(1) <<" times in the multimap\n";
cout<<"Key 2 appears " << mul.count(2) <<" times in the multimap\n";
return 0;
} आउटपुट
यदि हम उपरोक्त कोड चलाते हैं तो यह निम्न आउटपुट उत्पन्न करेगा -
Elements in multimap is : KEY ELEMENT 1 50 1 40 1 10 2 30 2 20 5 60 Key 1 appears 3 times in the multimap Key 2 appears 2 times in the multimap