কম্পিউটার

STL-এ মানচিত্র বাস্তবায়নের জন্য C++ প্রোগ্রাম


একটি মানচিত্র হল একটি সহযোগী ধারক যা একটি ম্যাপ করা ফ্যাশনে উপাদানগুলি সঞ্চয় করে। প্রতিটি উপাদানের একটি কী মান এবং একটি ম্যাপ করা মান রয়েছে। কোনো দুটি ম্যাপ করা মান একই কী মান থাকতে পারে না।

এখানে ফাংশন ব্যবহার করা হয়:

  • m::find() – পাওয়া গেলে ম্যাপে কী মান 'b' সহ উপাদানটিতে একটি পুনরাবৃত্তিকারী ফিরিয়ে দেয়, অন্যথায় পুনরাবৃত্তিকারীকে শেষ করে দেয়।

  • m::erase() – মানচিত্র থেকে মূল মান সরিয়ে দেয়।

  • m::equal_range() - জোড়ার একটি পুনরাবৃত্তিকারী প্রদান করে। পেয়ার বলতে বোঝায় একটি পরিসরের সীমানা যা কন্টেইনারের সমস্ত উপাদানগুলিকে অন্তর্ভুক্ত করে যার একটি কী সমতুল্য কী রয়েছে৷

  • m insert() – মানচিত্র পাত্রে উপাদান সন্নিবেশ করতে।

  • m size() – মানচিত্র পাত্রে উপাদানের সংখ্যা প্রদান করে।

  • m count() – মানচিত্রের মূল মান 'a' বা 'f' সহ উপাদানের সাথে মিলের সংখ্যা ফেরত দেয়।

উদাহরণ কোড

#include<iostream>
#include <map>
#include <string>
using namespace std;
int main () {
   map<char, int> 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 << ":";
      map<char, int>::iterator it;
      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";
   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:
a => 10
b => 20
c => 30
d => 40
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:
a => 10
c => 30
d => 40

  1. STL-এ সেট_ইউনিয়ন বাস্তবায়নের জন্য C++ প্রোগ্রাম

  2. STL-এ সেট_সিমেট্রিক_ডিফারেন্স বাস্তবায়নের জন্য C++ প্রোগ্রাম

  3. STL-এ Set_Intersection বাস্তবায়নের জন্য C++ প্রোগ্রাম

  4. STL-এ সেট_ডিফারেন্স বাস্তবায়নের জন্য C++ প্রোগ্রাম