এই আর্টিকেলে C++ STL-এ multiset::equal_range() ফাংশনের কাজ, সিনট্যাক্স এবং উদাহরণ আলোচনা করা হয়েছে।
C++ STL-এ multiset কী?
Multiset সেট কন্টেইনারের মতো একটি কন্টেইনার যা মানগুলো কি (key) রূপে একটি নির্দিষ্ট অর্ডারে সংরক্ষণ করে।
Multiset-এ মানগুলো সেটের মতো কি হিসেবে চিহ্নিত হয়। Multiset এবং সেটের প্রধান পার্থক্য হলো সেটে সব কি আলাদা থাকে (কোনো দুটি কি একসমান নয়), কিন্তু multiset-এ একই মানের কি থাকতে পারে।
Multiset কি বাইনারি সার্চ ট্রি বাস্তবায়ন করতে ব্যবহৃত হয়।
multiset::equal_range() কী?
multiset::equal_range() C++ STL-এ একটি বিল্ট-ইন ফাংশন যা <set> হেডার ফাইলে সংজ্ঞায়িত। equal_range() মাল্টিসেট কন্টেইনারে সমান এলিমেন্টগুলোর রেঞ্জ পাওয়ার জন্য ব্যবহৃত হয়।
এই ফাংশন সেই রেঞ্জের বাউন্ডস রিটার্ন করে যাতে কন্টেইনারের সব এলিমেন্ট থাকে যা ফাংশনে দেওয়া প্যারামিটারের সমান।
সিনট্যাক্স
ms_name.equal_range(value_type& val);
প্যারামিটার
- val - কন্টেইনারে যে মানের রেঞ্জ খুঁজছি।
রিটার্ন ভ্যালু
এই ফাংশন লোয়ার বাউন্ড এবং আপার বাউন্ডের একটি পেয়ার রিটার্ন করে যাদের মান প্যারামিটারের সমান।
উদাহরণ
ইনপুট
std::multiset<int> mymultiset = {1, 2, 2, 3, 4}; mymultiset.equal_range(2); আউটপুট
2 2
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
int main() {
multiset check;
check.insert(10);
check.insert(20);
check.insert(30);
check.insert(40);
check.insert(50);
check.insert(60);
check.insert(70);
check.insert(80);
cout << "Elements are: ";
for (auto i = check.begin(); i != check.end(); i++)
cout << *i << " ";
// lower bound and upper bound
auto i = check.equal_range(30);
cout << "\nThe lower bound of 30 is " << *i.first;
cout << "\nThe upper bound of 30 is " << *i.second;
// last element
i = check.equal_range(20);
cout << "\nThe lower bound of 20 is " << *i.first;
cout << "\nThe upper bound of 20 is " << *i.second;
i = check.equal_range(80);
cout << "\nThe lower bound of 80 is " << *i.first;
cout << "\nThe upper bound of 80 is " << *i.second;
return 0;
} আউটপুট
Elements are: 10 20 30 40 50 60 70 80 The lower bound of 30 is 30 The upper bound of 30 is 40 The lower bound of 20 is 20 The upper bound of 20 is 30 The lower bound of 80 is 80 The upper bound of 80 is 8