इस खंड में हम देखेंगे कि हम सी ++ के मानक पुस्तकालय का उपयोग करके कुछ सरणी या लिंक्ड सूची को कैसे सॉर्ट कर सकते हैं। सी ++ में कई अलग-अलग पुस्तकालय हैं जिनका उपयोग विभिन्न उद्देश्यों के लिए किया जा सकता है। छँटाई उनमें से एक है।
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