खोज…


टिप्पणियों

algorithm लाइब्रेरी में std::sort फ़ंक्शन फ़ैमिली पाई जाती है।

निर्दिष्ट क्रम के साथ अनुक्रम कंटेनर सॉर्ट करना

यदि कंटेनर में मानों को कुछ ऑपरेटर पहले से ही ओवरलोडेड हैं, तो std::sort का उपयोग विशेष प्रकार के फंक्शंस के साथ आरोही या अवरोही क्रम में सॉर्ट करने के लिए किया जा सकता है:

सी ++ 11
#include <vector>
#include <algorithm>
#include <functional>

std::vector<int> v = {5,1,2,4,3};

//sort in ascending order (1,2,3,4,5)
std::sort(v.begin(), v.end(), std::less<int>());

// Or just:
std::sort(v.begin(), v.end());

//sort in descending order (5,4,3,2,1)
std::sort(v.begin(), v.end(), std::greater<int>());

//Or just:
std::sort(v.rbegin(), v.rend());
सी ++ 14

C ++ 14 में, हमें तुलनात्मक फ़ंक्शन ऑब्जेक्ट्स के लिए टेम्प्लेट तर्क प्रदान करने की आवश्यकता नहीं है और इसके बजाय ऑब्जेक्ट को उसके आधार पर कटौती करने दें, जो उसमें पारित हो गया है:

std::sort(v.begin(), v.end(), std::less<>());     // ascending order
std::sort(v.begin(), v.end(), std::greater<>());  // descending order

ओवरलोडेड कम ऑपरेटर द्वारा अनुक्रम कंटेनरों को छांटना

यदि कोई आदेश देने वाला फ़ंक्शन पारित नहीं होता है, तो std::sort operator< के तत्वों को कॉल करके तत्वों का आदेश देगा operator< तत्वों के जोड़े पर, जो कि bool (या सिर्फ bool ) के लिए एक प्रकार का प्रासंगिक परिवर्तनीय लौटना चाहिए। मूल प्रकार (पूर्णांक, फ़्लोट्स, पॉइंटर्स आदि) पहले से ही तुलना ऑपरेटरों में निर्मित हैं।

उपयोगकर्ता-परिभाषित प्रकारों पर डिफ़ॉल्ट sort कॉल कार्य करने के लिए हम इस ऑपरेटर को अधिभारित कर सकते हैं।

// Include sequence containers
#include <vector>
#include <deque>
#include <list>

// Insert sorting algorithm
#include <algorithm>    

class Base {
 public:

    // Constructor that set variable to the value of v
    Base(int v): variable(v) {
    }
    
    // Use variable to provide total order operator less
    //`this` always represents the left-hand side of the compare.
    bool operator<(const Base &b) const { 
        return this->variable < b.variable;
    }
    
    int variable;
};

int main() {
    std::vector <Base> vector;
    std::deque <Base> deque;
    std::list <Base> list;
    
    // Create 2 elements to sort
    Base a(10);
    Base b(5);
    
    // Insert them into backs of containers
    vector.push_back(a);
    vector.push_back(b);
    
    deque.push_back(a);
    deque.push_back(b);
    
    list.push_back(a);
    list.push_back(b);
    
    // Now sort data using operator<(const Base &b) function
    std::sort(vector.begin(), vector.end());
    std::sort(deque.begin(), deque.end());
    // List must be sorted differently due to its design
    list.sort();

    return 0;
}

फंक्शन की तुलना करके अनुक्रम कंटेनरों को छाँटना

// Include sequence containers
#include <vector>
#include <deque>
#include <list>

// Insert sorting algorithm
#include <algorithm>

class Base {
 public:

    // Constructor that set variable to the value of v
    Base(int v): variable(v) {
    }
       
    int variable;
};

bool compare(const Base &a, const Base &b) {
    return a.variable < b.variable;
}

int main() {
    std::vector <Base> vector;
    std::deque <Base> deque;
    std::list <Base> list;
    
    // Create 2 elements to sort
    Base a(10);
    Base b(5);
    
    // Insert them into backs of containers
    vector.push_back(a);
    vector.push_back(b);
    
    deque.push_back(a);
    deque.push_back(b);
    
    list.push_back(a);
    list.push_back(b);
    
    // Now sort data using comparing function
    std::sort(vector.begin(), vector.end(), compare);
    std::sort(deque.begin(), deque.end(), compare);
    list.sort(compare);

    return 0;
}

लैम्बडा एक्सप्रेशंस (C ++ 11) का उपयोग करके अनुक्रम कंटेनरों को छाँटना

सी ++ 11
// Include sequence containers
#include <vector>
#include <deque>
#include <list>
#include <array>
#include <forward_list>

// Include sorting algorithm
#include <algorithm>

class Base {
 public:

    // Constructor that set variable to the value of v
    Base(int v): variable(v) {
    }
    
    int variable;
};


int main() {
    // Create 2 elements to sort
    Base a(10);
    Base b(5);
    
    // We're using C++11, so let's use initializer lists to insert items.
    std::vector <Base> vector = {a, b};
    std::deque <Base> deque = {a, b};
    std::list <Base> list = {a, b};
    std::array <Base, 2> array = {a, b};
    std::forward_list<Base> flist = {a, b};
    
    // We can sort data using an inline lambda expression
    std::sort(std::begin(vector), std::end(vector),
      [](const Base &a, const Base &b) { return a.variable < b.variable;});

    // We can also pass a lambda object as the comparator
    // and reuse the lambda multiple times
    auto compare = [](const Base &a, const Base &b) {
                     return a.variable < b.variable;};
    std::sort(std::begin(deque), std::end(deque), compare);
    std::sort(std::begin(array), std::end(array), compare);
    list.sort(compare);
    flist.sort(compare);

    return 0;
}

छँटाई और अनुक्रम कंटेनर

std::sort , मानक पुस्तकालय हेडर algorithm में पाया जाता algorithm , मानों की एक श्रृंखला को सॉर्ट करने के लिए एक मानक लाइब्रेरी एल्गोरिथ्म है, जो पुनरावृत्तियों की एक जोड़ी द्वारा परिभाषित किया गया है। std::sort अंतिम पैरामीटर के रूप में लेता है जो एक फ़ंक्टर का उपयोग दो मूल्यों की तुलना करने के लिए किया जाता है; यह इस तरह से आदेश को निर्धारित करता है। ध्यान दें कि std::sort स्थिर नहीं है

तुलना फ़ंक्शन को तत्वों पर एक सख्त, कमजोर आदेश देना चाहिए । एक साधारण कम से अधिक (या अधिक से अधिक) तुलना पर्याप्त होगी।

रैंडम-एक्सेस पुनरावृत्तियों के साथ एक कंटेनर को std::sort का उपयोग करके सॉर्ट किया जा सकता है std::sort एल्गोरिथ्म:

सी ++ 11
#include <vector>
#include <algorithm>

std::vector<int> MyVector = {3, 1, 2}

//Default comparison of <
std::sort(MyVector.begin(), MyVector.end());

std::sort आवश्यक है कि इसके चलने वाले यादृच्छिक अभिगमकर्ता हैं। अनुक्रम कंटेनर std::list और std::forward_list (सी ++ 11 की आवश्यकता होती है) यादृच्छिक अभिगम std::forward_list प्रदान नहीं करते हैं, इसलिए उन्हें std::sort साथ उपयोग नहीं किया जा सकता है। हालांकि, उनके पास sort सदस्य कार्य हैं जो एक सॉर्टिंग एल्गोरिथ्म को लागू करते हैं जो अपने स्वयं के इटेटर के साथ काम करता है।

सी ++ 11
#include <list>
#include <algorithm>

std::list<int> MyList = {3, 1, 2}

//Default comparison of <
//Whole list only.
MyList.sort();

उनके सदस्य sort फ़ंक्शंस हमेशा पूरी सूची को सॉर्ट करते हैं, इसलिए वे तत्वों की उप-श्रेणी को सॉर्ट नहीं कर सकते हैं। हालाँकि, चूंकि list और forward_list में तेजी से स्प्लिसिंग ऑपरेशन हैं, आप सूची से छांटे जाने वाले तत्वों को निकाल सकते हैं, उन्हें छांट सकते हैं, फिर उन्हें वापस वहीं ला सकते हैं जहाँ वे इस तरह से कुशलतापूर्वक थे:

void sort_sublist(std::list<int>& mylist, std::list<int>::const_iterator start, std::list<int>::const_iterator end) {
    //extract and sort half-open sub range denoted by start and end iterator 
    std::list<int> tmp;
    tmp.splice(tmp.begin(), list, start, end);
    tmp.sort();
    //re-insert range at the point we extracted it from
    list.splice(end, tmp);
}

एसटीडी के साथ छँटाई :: नक्शा (आरोही और अवरोही)

यह उदाहरण एक मानचित्र का उपयोग करके कुंजी के आरोही क्रम में तत्वों को सॉर्ट करता है। आप नीचे दिए गए उदाहरण में, std::string की जगह क्लास सहित किसी भी प्रकार का उपयोग कर सकते हैं।

#include <iostream>
#include <utility>
#include <map>

int main()
{
    std::map<double, std::string> sorted_map;
    // Sort the names of the planets according to their size
    sorted_map.insert(std::make_pair(0.3829, "Mercury"));
    sorted_map.insert(std::make_pair(0.9499, "Venus"));
    sorted_map.insert(std::make_pair(1,      "Earth"));
    sorted_map.insert(std::make_pair(0.532,  "Mars"));
    sorted_map.insert(std::make_pair(10.97,  "Jupiter"));
    sorted_map.insert(std::make_pair(9.14,   "Saturn"));
    sorted_map.insert(std::make_pair(3.981,  "Uranus"));
    sorted_map.insert(std::make_pair(3.865,  "Neptune"));

    for (auto const& entry: sorted_map)
    {
        std::cout << entry.second << " (" << entry.first << " of Earth's radius)" << '\n';
    }
}

आउटपुट:

Mercury (0.3829 of Earth's radius)
Mars (0.532 of Earth's radius)
Venus (0.9499 of Earth's radius)
Earth (1 of Earth's radius)
Neptune (3.865 of Earth's radius)
Uranus (3.981 of Earth's radius)
Saturn (9.14 of Earth's radius)
Jupiter (10.97 of Earth's radius)

यदि समान कुंजी के साथ प्रविष्टियां संभव हैं, तो map बजाय multimap उपयोग map (जैसे निम्नलिखित उदाहरण में)।

अवरोही तरीके से तत्वों को छाँटने के लिए, एक उचित तुलना फ़नकार ( std::greater<> ) के साथ मानचित्र घोषित करें:

#include <iostream>
#include <utility>
#include <map>

int main()
{
    std::multimap<int, std::string, std::greater<int>> sorted_map;
    // Sort the names of animals in descending order of the number of legs
    sorted_map.insert(std::make_pair(6,   "bug"));
    sorted_map.insert(std::make_pair(4,   "cat"));
    sorted_map.insert(std::make_pair(100, "centipede"));
    sorted_map.insert(std::make_pair(2,   "chicken"));
    sorted_map.insert(std::make_pair(0,   "fish"));
    sorted_map.insert(std::make_pair(4,   "horse"));
    sorted_map.insert(std::make_pair(8,   "spider"));

    for (auto const& entry: sorted_map)
    {
        std::cout << entry.second << " (has " << entry.first << " legs)" << '\n';
    }
}

उत्पादन

centipede (has 100 legs)
spider (has 8 legs)
bug (has 6 legs)
cat (has 4 legs)
horse (has 4 legs)
chicken (has 2 legs)
fish (has 0 legs)

अंतर्निहित सरणियों को क्रमबद्ध करना

sort एल्गोरिथ्म दो पुनरावृत्तियों द्वारा परिभाषित एक अनुक्रम sort करता है। यह बिल्ट-इन (सी-स्टाइल के रूप में भी जाना जाता है) सरणी को सॉर्ट करने के लिए पर्याप्त है।

सी ++ 11
int arr1[] = {36, 24, 42, 60, 59};

// sort numbers in ascending order
sort(std::begin(arr1), std::end(arr1));

// sort numbers in descending order
sort(std::begin(arr1), std::end(arr1), std::greater<int>());

C ++ 11 से पहले, सरणी के आकार का उपयोग करके सरणी का अंत "गणना" किया जाना था:

सी ++ 11
// Use a hard-coded number for array size
sort(arr1, arr1 + 5);

// Alternatively, use an expression
const size_t arr1_size = sizeof(arr1) / sizeof(*arr1);
sort(arr1, arr1 + arr1_size);


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow