এই বিভাগে আমরা দেখব কিভাবে আমরা C++ এর স্ট্যান্ডার্ড লাইব্রেরি ব্যবহার করে কিছু অ্যারে বা লিঙ্কযুক্ত তালিকা সাজাতে পারি। C++ এ একাধিক ভিন্ন লাইব্রেরি রয়েছে যা বিভিন্ন উদ্দেশ্যে ব্যবহার করা যেতে পারে। বাছাই তাদের মধ্যে একটি।
C++ ফাংশন std::list::sort() তালিকার উপাদানগুলিকে আরোহী ক্রমে সাজায়। সমান উপাদানের ক্রম সংরক্ষিত হয়। এটি তুলনা করার জন্য <অপারেটর ব্যবহার করে।
উদাহরণ
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 4, 2, 5, 3};
cout << "Contents of list before sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
l.sort();
cout << "Contents of list after sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
return 0;
} আউটপুট
Contents of list before sort operation 1 4 2 5 3 Contents of list after sort operation 1 2 3 4 5