সাধারণত C++ STL-এ map এবং multimap কন্টেইনারের ডিফল্ট আচরণ হলো উপাদানগুলো আরোহী ক্রমে (ascending order) সংরক্ষণ করা। কিন্তু আমরা std::greater কমপ্যারেটর ব্যবহার করে এগুলোকে অবরোহী ক্রমে (descending order) সংরক্ষণ করতে পারি।
মানচিত্র (Map) নিম্নক্রমে
ব্যবহৃত ফাংশনগুলো:
- m.find() – নির্দিষ্ট কী (key) মান 'b' সহ উপাদানটি মানচিত্রে পাওয়া গেলে সেটির ইটারেটর রিটার্ন করে, না পেলে
end()ইটারেটর রিটার্ন করে। - m.erase() – মানচিত্র থেকে নির্দিষ্ট কী মান মুছে ফেলে।
- m.equal_range() – একটি জোড়া (pair) ইটারেটর রিটার্ন করে যা নির্দিষ্ট কী সমতুল্য সব উপাদানের সীমাবদ্ধতা নির্দেশ করে।
- m.insert() – মানচিত্র কন্টেইনারে উপাদান সন্নিবেশ করার জন্য।
- m.size() – মানচিত্র কন্টেইনারের উপাদানের সংখ্যা রিটার্ন করে।
- m.count() – নির্দিষ্ট কী 'a' বা 'f' সহ উপাদানগুলোর মিলের সংখ্যা রিটার্ন করে।
উদাহরণ কোড
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// greater<char> ব্যবহার করে নিম্নক্রমে সাজানো map
map<char, int, greater<char>> m;
map<char, int>::iterator it;
m.insert(pair<char, int>('a', 10));
m.insert(pair<char, int>('b', 20));
m.insert(pair<char, int>('c', 30));
m.insert(pair<char, int>('d', 40));
cout << "Size of the map: " << m.size() << endl;
cout << "map contains:\n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
// প্রতিটি কী-এর জন্য উপাদান খুঁজে বের করা
for (char c = 'a'; c <= 'd'; c++) {
cout << "There are " << m.count(c) << " element(s) with key " << c << ":";
for (it = m.equal_range(c).first; it != m.equal_range(c).second; ++it)
cout << ' ' << (*it).second;
cout << endl;
}
// কী উপস্থিতি যাচাই
if (m.count('a'))
cout << "The key a is present\n";
else
cout << "The key a is not present\n";
if (m.count('f'))
cout << "The key f is present\n";
else
cout << "The key f is not present\n";
// 'b' কী খুঁজে মুছে ফেলা
it = m.find('b');
m.erase(it);
cout << "Size of the map: " << m.size() << endl;
cout << "map contains:\n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
আউটপুট
Size of the map: 4 map contains: d => 40 c => 30 b => 20 a => 10 There are 1 element(s) with key a: 10 There are 1 element(s) with key b: 20 There are 1 element(s) with key c: 30 There are 1 element(s) with key d: 40 The key a is present The key f is not present Size of the map: 3 map contains: d => 40 c => 30 a => 10
Multimap নিম্নক্রমে
multimap-এ একই কী (key) এর অধীনে একাধিক মান সংরক্ষণ করা সম্ভব। নিচে নিম্নক্রমে সাজানো multimap-এর উদাহরণ দেওয়া হলো।
ব্যবহৃত ফাংশনগুলো:
- mm.find() – নির্দিষ্ট কী 'b' সহ উপাদানটি multimap-এ পাওয়া গেলে ইটারেটর রিটার্ন করে।
- mm.erase() – multimap থেকে নির্দিষ্ট কী মান মুছে ফেলে।
- mm.equal_range() – নির্দিষ্ট কী সমতুল্য সব উপাদানের সীমাবদ্ধতা নির্দেশকারী ইটারেটর জোড়া রিটার্ন করে।
- mm.insert() – multimap কন্টেইনারে উপাদান সন্নিবেশ করে।
- mm.size() – multimap কন্টেইনারের উপাদান সংখ্যা রিটার্ন করে।
উদাহরণ কোড
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// greater<char> ব্যবহার করে নিম্নক্রমে সাজানো multimap
multimap<char, int, greater<char>> mm;
multimap<char, int>::iterator it;
mm.insert(pair<char, int>('a', 10));
mm.insert(pair<char, int>('b', 20));
mm.insert(pair<char, int>('a', 30));
mm.insert(pair<char, int>('b', 40));
cout << "Size of the multimap: " << mm.size() << endl;
cout << "multimap contains:\n";
for (it = mm.begin(); it != mm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
// প্রতিটি কী-এর জন্য সব উপাদান দেখানো
for (char c = 'a'; c <= 'd'; c++) {
cout << "There are " << mm.count(c) << " elements with key " << c << ":";
for (it = mm.equal_range(c).first; it != mm.equal_range(c).second; ++it)
cout << ' ' << (*it).second;
cout << endl;
}
// কী উপস্থিতি যাচাই
if (mm.count('a'))
cout << "The key a is present\n";
else
cout << "The key a is not present\n";
if (mm.count('f'))
cout << "The key f is present\n";
else
cout << "The key f is not present\n";
// প্রথম 'b' কীটি মুছে ফেলা
it = mm.find('b');
mm.erase(it);
cout << "Size of the multimap: " << mm.size() << endl;
cout << "multimap contains:\n";
for (it = mm.begin(); it != mm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
আউটপুট
Size of the multimap: 4 multimap contains: b => 20 b => 40 a => 10 a => 30 There are 2 elements with key a: 10 30 There are 2 elements with key b: 20 40 There are 0 elements with key c: There are 0 elements with key d: The key a is present The key f is not present Size of the multimap: 3 multimap contains: b => 40 a => 10 a => 30
মূল পার্থক্য: map-এ প্রতিটি কী уникаল (unique) হয়, কিন্তু multimap-এ একই কী-এর অধীনে একাধিক মান থাকতে পারে। greater<T> কমপ্যারেটর ব্যবহার করে যেকোনো associative কন্টেইনারকে নিম্নক্রমে সাজানো যায়।