কম্পিউটার

C++ এ একটি ভেক্টর বাছাই করা


C++ এ একটি ভেক্টর বাছাই করা std::sort() ব্যবহার করে করা যেতে পারে। এটি <অ্যালগরিদম> হেডারে সংজ্ঞায়িত করা হয়েছে। একটি স্থিতিশীল সাজানোর জন্য std::stable_sort ব্যবহার করা হয়। এটি ঠিক sort() এর মত কিন্তু সমান উপাদানের আপেক্ষিক ক্রম বজায় রাখে। Quicksort(), mergesort() প্রয়োজন অনুযায়ী ব্যবহার করা যেতে পারে।

অ্যালগরিদম

Begin
   Decalre v of vector type.
      Initialize some values into v in array pattern.
   Print “Elements before sorting”.
   for (const auto &i: v)
      print all the values of variable i.
   Print “Elements after sorting”.
   Call sort(v.begin(), v.end()) function to sort all the elements of the v vector.
   for (const auto &i: v)
      print all the values of variable i.
End.

এটি c++ এ একটি ভেক্টর সাজানোর একটি সহজ উদাহরণ:

উদাহরণ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
   vector<int> v = { 10, 9, 8, 6, 7, 2, 5, 1 };
   cout<<"Elements before sorting"<<endl;
   for (const auto &i: v)
      cout << i << ' '<<endl;
      cout<<"Elements after sorting"<<endl;
      sort(v.begin(), v.end());
   for (const auto &i: v)
      cout << i << ' '<<endl;
   return 0;
}

আউটপুট

Elements before sorting
10
9
8
6
7
2
5
1
Elements after sorting
1
2
5
6
7
8
9
10

  1. কিভাবে C++ এ একটি ভেক্টর শুরু করবেন?

  2. ভেক্টর::রিসাইজ() বনাম ভেক্টর::রিজার্ভ() C++ এ

  3. C++ এ ইনফারেন্স টাইপ করুন

  4. C++ এ সাজানো হচ্ছে