Home > Backend Development > C++ > body text

How to use sort function in c++

小老鼠
Release: 2024-03-25 17:58:47
Original
1070 people have browsed it

The sort function in C is a useful STL algorithm library function used to sort elements in a container. Its basic syntax is: `sort(Iterator first, Iterator last)`, where first and last are iterators that define the starting and ending positions of the sequence. By default, the sort function sorts in ascending order, but you can customize the sorting by providing a comparison function or overloading `operator<`. For objects of custom types, comparison functions need to be provided.

How to use sort function in c++

In C, the sort function is a very useful function in the STL (Standard Template Library) algorithm library. It is used to sort elements in the container. Sort. This function is defined in the header file, so you need to include this header file before using it.

The basic syntax of the sort function is as follows:

cpp

#include <algorithm>  
#include <vector>  
  
std::sort(Iterator first, Iterator last);
Copy after login

Here, first and last are iterators, which define the starting and ending positions of the sequence to be sorted. Note that the last iterator points to the next element at the "end position" of the sequence, so the actual range of the sequence is [first, last).

The sort function sorts elements in ascending order by default. If you need to sort objects of a custom type, you may need to provide a comparison function or overload operator<.

The following is a simple example that demonstrates how to use the sort function to sort a vector:

cpp

#include <iostream>  
#include <vector>  
#include <algorithm>  
  
int main() {  
    std::vector<int> numbers = {5, 2, 8, 1, 9};  
      
    std::sort(numbers.begin(), numbers.end());  
      
    for (int num : numbers) {  
        std::cout << num << &#39; &#39;;  
    }  
      
    return 0;  
}
Copy after login

This program will output: 1 2 5 8 9, this is the result of the elements in the numbers vector being arranged in ascending order.

If you need to sort objects of a custom type, you need to provide a comparison function or overload operator<. For example, suppose you have a Person class that has an age member variable, and you want to sort Person objects by age:

cpp

#include <iostream>  
#include <vector>  
#include <algorithm>  
  
class Person {  
public:  
    std::string name;  
    int age;  
      
    Person(const std::string& name, int age) : name(name), age(age) {}  
      
    // 重载 operator< 以便 sort 可以使用  
    bool operator<(const Person& other) const {  
        return age < other.age;  
    }  
};  
  
int main() {  
    std::vector<Person> people = {  
        {"Alice", 30},  
        {"Bob", 20},  
        {"Charlie", 25}  
    };  
      
    std::sort(people.begin(), people.end());  
      
    for (const auto& person : people) {  
        std::cout << person.name << ": " << person.age << std::endl;  
    }  
      
    return 0;  
}
Copy after login

This program will output each person's age in ascending order Name and age. Note that we overload operator< so that the sort function knows how to compare Person objects. If you don't want to overload operator

The above is the detailed content of How to use sort function in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template