📅  最后修改于: 2023-12-03 14:39:52.193000             🧑  作者: Mango
std::sort()
in C++ STLIn C++, the Standard Template Library (STL) provides a set of powerful algorithms and data structures to simplify and enhance programming. One of the frequently used algorithms is std::sort()
, which is used to sort elements in a container in ascending order. In this guide, we will explore the usage of std::sort()
and delve into its details.
The syntax of std::sort()
is as follows:
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
first
and last
are iterators representing the range of elements to be sorted. It sorts the elements in the range [first, last)
in ascending order.comp
is an optional parameter that specifies a custom comparison function. It enables sorting based on criteria other than the default less-than operator (<
).To sort the elements in a container using std::sort()
, you need to provide the appropriate iterators that define the range of elements. Here's an example of sorting a std::vector
using std::sort()
:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {5, 2, 9, 1, 3};
std::sort(numbers.begin(), numbers.end());
for (const auto& num : numbers) {
std::cout << num << " ";
}
return 0;
}
Output:
1 2 3 5 9
In this example, we include the necessary headers and create a std::vector
called numbers
with some random unsorted values. We then call std::sort()
on the range defined by numbers.begin()
and numbers.end()
. Finally, we output the sorted elements using a range-based for loop.
std::sort()
allows you to define a custom comparison function to determine the sorting order. The sorting criteria are based on the return values of the comparison function. Here's an example that sorts a std::vector
of strings in descending order of length:
#include <iostream>
#include <vector>
#include <algorithm>
bool compareByLength(const std::string &str1, const std::string &str2) {
return str1.length() > str2.length();
}
int main() {
std::vector<std::string> words = {"apple", "banana", "cat", "dog"};
std::sort(words.begin(), words.end(), compareByLength);
for (const auto& word : words) {
std::cout << word << " ";
}
return 0;
}
Output:
banana apple cat dog
In this example, we define a custom comparison function compareByLength
that returns true if str1
has a greater length than str2
. We call std::sort()
on the range defined by words.begin()
and words.end()
, passing the compareByLength
function as the third argument. This allows us to sort the strings based on their lengths in descending order.
The std::sort()
algorithm uses the introsort algorithm, which is a hybrid between quicksort, heapsort, and insertion sort. The time complexity of std::sort()
is generally O(n log n), where n is the number of elements in the sequence to be sorted. Some versions of the library may use a variation of quicksort with a worst-case time complexity of O(n^2).
The std::sort()
algorithm in C++ STL is a powerful tool for sorting elements in a container. By default, it sorts elements in ascending order using the less-than operator (<
). However, you can provide a custom comparison function to define your own sorting criteria. Understanding the usage and capabilities of std::sort()
is essential for any C++ programmer seeking efficient and reliable sorting operations.