C++ STL में Multimap क्या है?
Multimap एक associative container है जो map container के समान होता है। यह key-value और mapped value के संयोजन से बने तत्वों को एक विशिष्ट क्रम में संग्रहीत करता है। एक multimap container में एक ही key से जुड़े कई तत्व हो सकते हैं। डेटा आंतरिक रूप से हमेशा उससे जुड़ी keys की मदद से सॉर्ट किया जाता है।
multimap::count() क्या है?
Multimap::count() C++ STL में एक अंतर्निहित फंक्शन है, जो <map> हेडर फाइल में परिभाषित है। count() का उपयोग किसी विशिष्ट key के साथ multimap में मौजूद तत्वों की संख्या गिनने के लिए किया जाता है।
यदि key multimap container में मौजूद नहीं है तो यह फंक्शन शून्य लौटाता है।
सिंटैक्स
multimap_name.count(key_type& key);
पैरामीटर्स
key - यह वह key है जिसे हम खोजना चाहते हैं और उससे जुड़े तत्वों की संख्या गिनना चाहते हैं।
रिटर्न वैल्यू
यह फंक्शन एक integer लौटाता है यानी समान key वाले तत्वों की संख्या।
उदाहरण
#include <bits/stdc++.h>
using namespace std;
int main() {
// container बनाएं
multimap<int, int> mul;
// 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