Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

std::sort(vector.begin(), vector.end(), compare); std::sort(deque.begin(), deque.end(), compare); list.sort(compare);

return 0;

}

Section 67.5: Sorting sequence containers using lambda expressions (C++11)

Version ≥ C++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;

}

GoalKicker.com – C++ Notes for Professionals

353

Section 67.6: Sorting built-in arrays

The sort algorithm sorts a sequence defined by two iterators. This is enough to sort a built-in (also known as c- style) array.

Version ≥ C++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>());

Prior to C++11, end of array had to be "calculated" using the size of the array:

Version < C++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);

Section 67.7: Sorting sequence containers with specifed ordering

If the values in a container have certain operators already overloaded, std::sort can be used with specialized functors to sort in either ascending or descending order:

Version ≥ C++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());

Version ≥ C++14

In C++14, we don't need to provide the template argument for the comparison function objects and instead let the object deduce based on what it gets passed in:

std::sort(v.begin(),

v.end(),

std::less<>());

//

ascending order

std::sort(v.begin(),

v.end(),

std::greater<>());

//

descending order

 

 

 

 

 

GoalKicker.com – C++ Notes for Professionals

354