1. vector 的 front() 和 back()
vector.front() 和 vector.back() 在空容器上调用会导致未定义的行为。
所以,如果想要使用 front() 和 back(),需要满足容器不为空。
if (!arr.empty())
{
// arr.front();
// arr.back();
}
2. unordered_map 赋值后不保证顺序
unordered_map 是基于哈希表的无序结构,在将 unordered_map 对象赋值给一个新的 unordered_map 时,前后两个 unordered_map 的元素顺序是不一致的。
std::unordered_map<int, std::string> um;
um.emplace(1001, "111");
um.emplace(1012, "222");
um.emplace(1023, "333");
um.emplace(1044, "444");
um.emplace(1005, "555");
um.emplace(1016, "666");
for (auto const & i : um) {
std::cout << i.first << " " << i.second << std::endl;
}
std::cout << "----------------------" << std::endl;
auto new_um = um;
for (auto const& i : new_um) {
std::cout << i.first << " " << i.second << std::endl;
}
// 结果
// 1044 444
// 1001 111
// 1012 222
// 1023 333
// 1005 555
// 1016 666
// ----------------------
// 1001 111
// 1044 444
// 1012 222
// 1023 333
// 1005 555
// 1016 666
3. 获取对象类型
C++ 中使用typeid()来获取对象的类型信息
#include <iostream>
#include <typeinfo>
int main() {
int i;
std::cout << "i type is: " << typeid(i).name() << '\n';
return 0;
}
// 结果
// i is: int
4. decltype
decltype用于选择并返回操作数的数据类型,解决复杂的类型声明。
- 作用于变量:得到变量的类型
- 作用于表达式:左值的表达式得到类型的引用;右值的表达式得到类型。且都不计算表达式的值
- 作用于函数名:得到函数类型,不转换成指针
auto、typeid、decltype区别:
- auto:推到类型,且必须初始化
- typeid:获取类型,可用于类型的比较
- decltype:获取类型,仅用于声明
decltype的使用场景:用于声明模板函数,此模板函数的返回值类型依赖于其参数类型
// C++ 11
template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u)
{
return t + u;
}
// C++ 14
template<typename T, typename U>
auto add(T t, U u)
{
return t + u;
}
// 在其所调用的函数返回引用的情况下
// 函数调用的完美转发必须用 decltype(auto)
template<class F, class... Args>
decltype(auto) PerfectForward(F fun, Args&&... args)
{
return fun(std::forward<Args>(args)...);
}
// C++17 支持 auto 形参声明
template<auto n>
auto f() -> std::pair<decltype(n), decltype(n)> // auto 不能从花括号初始化器列表推导
{
return { n, n + 1 };
}
int main()
{
auto arr = f<9>();
std::cout << "arr.first :" << arr.first << " arr.second :" << arr.second << std::endl;
return 0;
}
// 结果
// arr.first :9 arr.second :10