Phần 1
1. Goal
After learning basic lambda expressions, you should understand:
1. Why C++11 introduced lambdas.
2. The minimum lambda syntax.
3. How to use lambdas with STL algorithms.
4. Common mistakes when writing basic lambdas.
5. How lambdas improve readability in real-world C++ code.Phần 2
2. What Problem Does Lambda Solve?
A lambda is a small unnamed function that can be written directly where it is needed.
Before lambdas, simple custom logic often required a separate function or function object.
With lambdas, you can write local behavior inline:
auto isLargeOrder = [](int quantity) {
return quantity > 1000;
};Main idea:
Use lambda when you need a small function for local, short-lived logic.Phần 3
3. Minimum Syntax
Basic lambda syntax:
[]() {
// body
};Lambda with parameters:
[](int x) {
return x > 0;
};Store a lambda in a variable:
auto isPositive = [](int x) {
return x > 0;
};Call it:
isPositive(10);Lambda used directly in an algorithm:
std::sort(v.begin(), v.end(), [](int a, int b) {
return a < b;
});Phần 4
4. Basic Structure
A lambda has this general shape:
[capture](parameters) -> return_type {
body
}For basic lambdas, focus on:
[] = capture list, empty for now
(parameters) = input values
body = logic
return = resultExample:
auto isBuyOrder = [](int side) {
return side == 1;
};Phần 5
5. Common Mistakes
5.1. Forgetting to call the lambda
auto f = []() {
return 10;
};This only creates the lambda.
To execute it:
int x = f();5.2. Writing unclear lambdas
Avoid putting too much logic inside one lambda.
A lambda should usually be short and focused.
5.3. Using lambda when a named function is clearer
If the logic is reused many times or is domain-important, a named function may be better.
5.4. Forgetting the return value
For predicates, the lambda must return true or false.
auto isActive = [](int status) {
return status == 1;
};Phần 6
6. When Should You Use Lambda?
Use lambda when:
1. The logic is short.
2. The logic is used locally.
3. You need a custom condition.
4. You need a custom sort rule.
5. You are using STL algorithms.Avoid lambda when:
1. The logic is long.
2. The logic is reused in many places.
3. A clear named function would be easier to read.Phần 7
7. Notes for Trading / Performance-Sensitive Code
Trading code often needs small local rules:
Sort orders by price.
Filter large trades.
Find active orders.
Check whether quantity exceeds a threshold.Lambdas are useful for these local rules:
auto isLargeOrder = [](const Order& order) {
return order.quantity > 1000;
};They make code concise without creating many small one-time functions.
Phần 8
8. End-of-Day Checklist
You should remember:
[](...) { ... }You should be able to answer:
1. What is a lambda?
2. Why is a lambda useful?
3. What does [] mean?
4. How do you store and call a lambda?
5. When should you avoid using a lambda?Phần 9
9. Conclusion
A lambda is a small unnamed function written directly where it is needed.
The key idea:
Use lambdas for short, local logic.
Keep basic lambdas simple before learning captures and mutable lambdas.:::
Mã mẫu hoàn chỉnh
cpp11/7_lambda basic/main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Order {
int id;
std::string symbol;
double price;
int quantity;
};
int main() {
std::vector<Order> orders{
{101, "AAPL", 191.25, 100},
{102, "MSFT", 420.50, 1500},
{103, "NVDA", 875.10, 300}
};
std::sort(orders.begin(), orders.end(), [](const Order& a, const Order& b) {
return a.price < b.price;
});
for (const auto& order : orders) {
std::cout << order.id << " "
<< order.symbol << " "
<< order.price << " "
<< order.quantity << '\n';
}
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/5 mục