Phần 1
1. Goal
After learning using alias, you should understand:
1. Why C++11 introduced `using` alias.
2. How it improves readability compared to long type names.
3. The difference between `using` and `typedef`.
4. Common mistakes when creating aliases.
5. How aliases help in real-world C++ code.Phần 2
2. What Problem Does It Solve?
In C++, type names can become long and hard to read, especially with STL containers.
Example:
std::unordered_map<std::string, std::vector<double>>A using alias gives a readable name to a type:
using PriceHistory = std::unordered_map<std::string, std::vector<double>>;Main idea:
Give a complex type a clear, meaningful name.This makes code shorter, easier to read, and easier to maintain.
Phần 3
3. Minimum Syntax
Basic alias:
using NewName = ExistingType;Examples:
using Price = double;
using Quantity = int;
using Symbol = std::string;Alias for STL containers:
using PriceHistory = std::unordered_map<std::string, std::vector<double>>;Alias for function pointers:
using Handler = void(*)(int);Alias template:
template <typename T>
using Vec = std::vector<T>;Phần 4
4. using vs typedef
Old style:
typedef long long ll;Modern C++ style:
using ll = long long;For simple types, both work.
But using is usually clearer, especially for complex types and templates.
Alias templates are much cleaner with using:
template <typename T>
using Vec = std::vector<T>;Phần 5
5. Common Mistakes
5.1. Hiding important domain meaning
using Price = double;
using Quantity = double;Both are double, so the compiler cannot prevent mixing them.
Aliases improve readability, but they do not create new strong types.
5.2. Overusing aliases
Too many aliases can make code harder to understand.
Use aliases only when they make the code clearer.
5.3. Confusing alias with a new type
using Price = double;This does not create a new independent type.
It is just another name for double.
Phần 6
6. When Should You Use using Alias?
Use using when:
1. A type name is too long.
2. The alias improves domain readability.
3. You want to simplify STL container types.
4. You want cleaner function pointer declarations.
5. You want alias templates.Avoid it when:
1. The original type is already simple.
2. The alias hides too much information.
3. Too many aliases make the code harder to navigate.Phần 7
7. Notes for Trading / Performance-Sensitive Code
Trading code often has domain concepts such as:
Price
Quantity
Symbol
OrderId
Timestamp
OrderBookAliases can make code more readable:
using Price = double;
using Quantity = int;
using Symbol = std::string;However, remember:
A type alias is not a new strong type.
It only gives another name to an existing type.For stronger type safety, you need wrapper structs or strong typedef patterns.
Phần 8
8. End-of-Day Checklist
You should remember:
using NewName = ExistingType;You should be able to answer:
1. Why is `using` useful?
2. How is it different from `typedef`?
3. Does `using Price = double` create a new type?
4. When can aliases make code harder to read?
5. Why are alias templates useful?Phần 9
9. Conclusion
using alias is a small but practical C++11 feature.
The key idea:
Use `using` to give long or domain-specific types clearer names.
Do not overuse it, and remember that an alias is not a new type.:::
Mã mẫu hoàn chỉnh
cpp11/4_using alias/main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <iomanip>
using Symbol = std::string;
using Price = double;
using Quantity = int;
using PriceHistory = std::unordered_map<Symbol, std::vector<Price>>;
using PositionMap = std::unordered_map<Symbol, Quantity>;
struct Tick {
Symbol symbol;
Price price;
Quantity quantity;
};
void addTick(PriceHistory& history, const Tick& tick) {
history[tick.symbol].push_back(tick.price);
}
void updatePosition(PositionMap& positions, const Tick& tick) {
positions[tick.symbol] += tick.quantity;
}
void printPriceHistory(const PriceHistory& history) {
std::cout << "Price history:\n";
for (const auto& item : history) {
const Symbol& symbol = item.first;
const std::vector<Price>& prices = item.second;
std::cout << symbol << ": ";
for (Price price : prices) {
std::cout << std::fixed << std::setprecision(2)
<< price << " ";
}
std::cout << '\n';
}
}
void printPositions(const PositionMap& positions) {
std::cout << "\nPositions:\n";
for (const auto& item : positions) {
const Symbol& symbol = item.first;
Quantity quantity = item.second;
std::cout << symbol << " -> " << quantity << '\n';
}
}
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}
};
PriceHistory history;
PositionMap positions;
for (const auto& tick : ticks) {
addTick(history, tick);
updatePosition(positions, tick);
}
printPriceHistory(history);
printPositions(positions);
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