Phần 1
1. Goal
After learning auto, you should understand:
1. What problem `auto` solves.
2. The minimum syntax of `auto`.
3. When to use `auto`, `auto&`, and `const auto&`.
4. Common mistakes when using `auto`.
5. How to use `auto` in real-world code, especially performance-sensitive code.Phần 2
2. What problem does auto solve?
auto allows the compiler to automatically infer the data type from the right-hand side.
Example:
auto x = 10; // int
auto y = 3.14; // double
auto it = v.begin(); // iteratorBefore C++11, many types were very long, especially iterator types:
std::vector<std::pair<std::string, double>>::iterator it = prices.begin();Using auto:
auto it = prices.begin();auto helps with:
1. Shorter code.
2. Avoiding long type names.
3. Avoiding mistakes when writing iterator types.
4. Making code easier to maintain when return types change.But remember carefully:
`auto` is not a dynamic type like in Python.The type is still determined at compile time.
Example:
auto x = 10; // x is int
x = 3.14; // 3.14 is converted to intPhần 3
3. Minimum syntax
3.1. Normal variables
auto x = 10; // int
auto y = 3.14; // double
auto c = 'A'; // char
auto s = "ABC"; // const char*Be careful:
auto s = "ABC";s is not std::string; it is const char*.
If you want std::string:
auto s = std::string("ABC");3.2. Using auto with iterators
auto it = v.begin();Instead of writing a long type like:
std::vector<int>::iterator it = v.begin();3.3. Using auto in range-based for loops
for (auto x : v)x is a copy.
for (auto& x : v)x is a reference, so it can modify the actual element.
for (const auto& x : v)x is a read-only reference, avoiding unnecessary copies.
Phần 4
4. Difference between auto, auto&, and const auto&
This is the most important part.
auto x = value;This means copy.
auto& x = value;This means reference, so it can modify the original variable.
const auto& x = value;This means read-only reference, avoiding unnecessary copies.
Summary:
auto = copy
auto& = reference
const auto& = read-only referencePhần 5
5. Common mistakes when using auto
5.1. Thinking auto is a dynamic type
Wrong:
auto x = 10;
x = "hello";Reason: x is already an int.
5.2. Forgetting &, so the original data is not modified
Wrong if you want to modify the actual vector:
for (auto x : v) {
x = 0;
}Because x is only a copy.
Correct:
for (auto& x : v) {
x = 0;
}5.3. Copying heavy objects
Not recommended if the object is large:
for (auto tick : ticks)Because each loop iteration copies one tick.
Better if you only read the data:
for (const auto& tick : ticks)In trading code, data such as ticks, order books, and trade events can be very large. Unnecessary copying can reduce performance.
5.4. Confusing string literals with std::string
auto symbol = "AAPL";symbol is const char*, not std::string.
If you want a real string:
auto symbol = std::string("AAPL");5.5. Overusing auto, making code harder to read
You should not write:
auto x = f();if it is unclear what f() returns.
Better:
OrderBook book = loadOrderBook();Or if you still use auto, give the variable a clear name:
auto best_bid_price = order_book.bestBidPrice();Phần 6
6. When should you use auto?
Use auto when:
1. The type is too long.
2. You are using iterators.
3. You are iterating through a container with a range-based for loop.
4. The right-hand side already makes the type clear.
5. You are using lambdas or generic code.Good examples:
auto it = prices.begin();
auto price = order.bestBidPrice();Phần 7
7. When should you avoid using auto?
Avoid using auto when:
1. The type is important for the reader to understand immediately.
2. The right-hand side is an unclear function such as f(), get(), or parse().
3. It may cause confusion, such as auto s = "ABC".
4. The code needs to clearly express a domain-specific type.Example where explicit types are better:
OrderBook book = loadOrderBook();
Price price = parsePrice(raw);
Quantity qty = parseQuantity(raw);Phần 8
8. Notes for trading / performance-sensitive code
In code that processes large amounts of data:
for (auto x : container)may cause unnecessary copies.
If you only read the data, prefer:
for (const auto& x : container)If you need to modify the actual element:
for (auto& x : container)If the object is small, such as int or double, copying is fine:
for (auto x : numbers)But if the object is large, such as Tick, Order, Trade, std::string, or std::vector, you should be careful.
Phần 9
9. End-of-day checklist
You must remember:
auto x = value; // copy
auto& x = value; // reference
const auto& x = value; // read-only reference, avoids copyingYou should be able to answer:
1. Is `auto` a dynamic type?
2. What is the difference between `auto` and `auto&`?
3. Why is `const auto&` commonly used when iterating through containers?
4. Why is `auto s = "ABC"` not `std::string`?
5. When should you avoid overusing `auto`?Phần 10
10. Conclusion
auto is a small but very important feature in Modern C++.
Understanding auto correctly is not just about knowing that the compiler can infer the type. You also need to know:
1. When it copies.
2. When it keeps a reference.
3. When it makes code shorter.
4. When it makes code harder to read.
5. When it affects performance.The most important idea for Day 1:
In range-based for loops:
- `auto` copies.
- `auto&` can modify the original object.
- `const auto&` is read-only and avoids copying.Mã mẫu hoàn chỉnh
cpp11/1_auto/main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <iomanip>
struct Tick{
std::string symbol;
double price;
int quantity;
};
int main(){
std::vector<Tick> ticks = {
{"AAPL", 191.25, 100},
{"MSFT", 420.50, 50},
{"AAPL", 191.40, 200},
{"NVDA", 875.10, 10},
{"MSFT", 421.00, 70}
};
std::unordered_map<std::string, double> last_price;
std::unordered_map<std::string, int> total_quantity;
for(const auto& tick : ticks){
last_price[tick.symbol] = tick.price;
total_quantity[tick.symbol] += tick.quantity;
}
for(auto it = last_price.begin(); it != last_price.end(); ++it){
const auto& symbol = it->first;
auto price = it->second;
std::cout << symbol << " -> "
<< std::fixed << std::setprecision(2)
<< price << '\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