কম্পিউটার টিউটোরিয়াল

C++ STL-এ lexicographical_compare ইমপ্লিমেন্টেশন

C++ ফাংশন std::algorithm::lexicographical_compare() যাচাই করে যে একটি রেঞ্জ (range) অন্য রেঞ্জের চেয়ে লেক্সিকোগ্রাফিক্যালি (lexicographically) ছোট কিনা। লেক্সিকোগ্রাফিক্যাল তুলনা হলো ডিকশনারিতে শব্দগুলো বর্ণানুক্রমে সর্ট করার জন্য ব্যবহৃত তুলনার পদ্ধতি।

ডিক্লেয়ারেশন (Declaration)

template <class InputIterator1, class InputIterator2>

bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);

অ্যালগরিদম

শুরু
  result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
  যদি (result == true) হয়, তবে
    "v1 is less than v2" প্রিন্ট করুন
  result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
  যদি (result == false) হয়, তবে
    "v1 is not less than v2" প্রিন্ট করুন
শেষ

উদাহরণ কোড

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main(void) {
    // v1 এবং v2 ইনিশিয়ালাইজেশন
    vector<string> v1 = {"One", "Two", "Three"};
    vector<string> v2 = {"one", "two", "three"};
    bool result;
    
    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
    if (result == true)
        cout << "v1 is less than v2." << endl;
    
    v1[0] = "two";
    result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
    if (result == false)
        cout << "v1 is not less than v2." << endl;
    
    return 0;
}

আউটপুট

v1 is less than v2.
v1 is not less than v2.
  1. C++ STL-এ Set Union বাস্তবায়ন: দুটি সেটের ইউনিয়ন

  2. C++ STL-এ Set Symmetric Difference বাস্তবায়ন

  3. C++ STL-এ Set Intersection বাস্তবায়ন: দুটি সেটের অন্তঃসেট

  4. C++ STL-এ Set Difference বাস্তবায়ন: সেট বিয়োগ