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

bookFunction Composition

Function composition is a powerful technique that lets you combine multiple functions or predicates to build more complex operations from simpler ones. In C++, you can achieve function composition and chaining by using lambdas and standard algorithms. This approach is especially useful when you want to apply a series of transformations or filters to collections, such as vectors, in a concise and expressive way. By composing functions, you can create pipelines where the output of one function becomes the input of the next, making your code modular and easier to maintain.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // First lambda: filter even numbers auto is_even = [](int x) { return x % 2 == 0; }; // Second lambda: square a number auto square = [](int x) { return x * x; }; // Compose: filter then transform std::vector<int> evens; std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), is_even); std::vector<int> squared_evens; std::transform(evens.begin(), evens.end(), std::back_inserter(squared_evens), square); std::cout << "Squared even numbers: "; for (int n : squared_evens) { std::cout << n << " "; } std::cout << std::endl; return 0; }
question mark

What does function composition enable you to do when working with lambdas in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookFunction Composition

Svep för att visa menyn

Function composition is a powerful technique that lets you combine multiple functions or predicates to build more complex operations from simpler ones. In C++, you can achieve function composition and chaining by using lambdas and standard algorithms. This approach is especially useful when you want to apply a series of transformations or filters to collections, such as vectors, in a concise and expressive way. By composing functions, you can create pipelines where the output of one function becomes the input of the next, making your code modular and easier to maintain.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // First lambda: filter even numbers auto is_even = [](int x) { return x % 2 == 0; }; // Second lambda: square a number auto square = [](int x) { return x * x; }; // Compose: filter then transform std::vector<int> evens; std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), is_even); std::vector<int> squared_evens; std::transform(evens.begin(), evens.end(), std::back_inserter(squared_evens), square); std::cout << "Squared even numbers: "; for (int n : squared_evens) { std::cout << n << " "; } std::cout << std::endl; return 0; }
question mark

What does function composition enable you to do when working with lambdas in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
some-alt