Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Algorithm Parameters | Working with Predicates and Algorithms
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Functional Utilities

bookAlgorithm Parameters

STL algorithms such as std::sort are designed to accept callable parameters, which means you can pass lambdas or function objects to customize their behavior. This feature allows you to define exactly how elements should be compared, transformed, or filtered during algorithm execution. By providing a lambda, you can write concise, inline logic that is tailored to your specific task, without the need to define separate functions or classes. This leads to more expressive, maintainable, and flexible code, as you can adjust algorithm behavior directly at the call site.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <algorithm> #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"}; // Sort names by length using a lambda as a custom comparator std::sort(names.begin(), names.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); } ); for (const auto& name : names) { std::cout << name << " "; } std::cout << std::endl; return 0; }
question mark

What is the main advantage of passing a lambda as a parameter to an STL algorithm like std::sort?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you give an example of using a lambda with std::sort?

What are some other STL algorithms that accept lambdas?

Why would I use a lambda instead of a regular function?

bookAlgorithm Parameters

Deslize para mostrar o menu

STL algorithms such as std::sort are designed to accept callable parameters, which means you can pass lambdas or function objects to customize their behavior. This feature allows you to define exactly how elements should be compared, transformed, or filtered during algorithm execution. By providing a lambda, you can write concise, inline logic that is tailored to your specific task, without the need to define separate functions or classes. This leads to more expressive, maintainable, and flexible code, as you can adjust algorithm behavior directly at the call site.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <algorithm> #include <iostream> #include <vector> #include <string> int main() { std::vector<std::string> names = {"Alice", "Bob", "Charlie", "David"}; // Sort names by length using a lambda as a custom comparator std::sort(names.begin(), names.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); } ); for (const auto& name : names) { std::cout << name << " "; } std::cout << std::endl; return 0; }
question mark

What is the main advantage of passing a lambda as a parameter to an STL algorithm like std::sort?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
some-alt