Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Sorting Algorithms | Algorithms
C++ STL Containers and Algorithms

bookSorting Algorithms

Efficiently organizing data is essential in software development. In C++, the Standard Template Library (STL) provides robust, highly optimized algorithms for sorting sequences. The two primary sorting algorithms are std::sort and std::stable_sort.

Both algorithms accept a custom comparator, allowing you to define exactly how elements should be ordered. The comparator must define a strict weak ordering and should be consistent and free of side effects.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829
#include <algorithm> #include <iostream> #include <string> #include <vector> struct Person { std::string name; int age; }; int main() { std::vector<Person> people = { {"Alice", 30}, {"Bob", 25}, {"Charlie", 35}, {"Diana", 25} }; // Sort by age, then by name std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) { if (a.age != b.age) return a.age < b.age; return a.name < b.name; }); for (const auto& person : people) std::cout << person.name << " (" << person.age << ")\n"; }
Note
Note

Use std::stable_sort when you need to preserve the relative order of equal elements.

question mark

Which statement best describes the key difference between std::sort and std::stable_sort in C++ STL?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 6.67

bookSorting Algorithms

Свайпніть щоб показати меню

Efficiently organizing data is essential in software development. In C++, the Standard Template Library (STL) provides robust, highly optimized algorithms for sorting sequences. The two primary sorting algorithms are std::sort and std::stable_sort.

Both algorithms accept a custom comparator, allowing you to define exactly how elements should be ordered. The comparator must define a strict weak ordering and should be consistent and free of side effects.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829
#include <algorithm> #include <iostream> #include <string> #include <vector> struct Person { std::string name; int age; }; int main() { std::vector<Person> people = { {"Alice", 30}, {"Bob", 25}, {"Charlie", 35}, {"Diana", 25} }; // Sort by age, then by name std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) { if (a.age != b.age) return a.age < b.age; return a.name < b.name; }); for (const auto& person : people) std::cout << person.name << " (" << person.age << ")\n"; }
Note
Note

Use std::stable_sort when you need to preserve the relative order of equal elements.

question mark

Which statement best describes the key difference between std::sort and std::stable_sort in C++ STL?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
some-alt