工作小结 22.04.07

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 抱佛脚一时爽,一直抱佛脚一直爽!这篇文章总结常见的c++面试问题~因为是抱佛脚,所以结构上没有什么逻辑...参考链...
    山幺幺阅读 806评论 0 0
  • c++学习文档汇总 [TOC] c++ primer部分 c++学习笔记 引用(左值引用) int &ref...
    flyrain阅读 477评论 0 0
  • 自动类型推导 现代的编程语言,不管是动态语言(JavaScript、Python 等),还是静态语言(Go、Rus...
    linjinhe阅读 933评论 0 0
  • 官方答案2 思路:和常规方法一样,还是通过查找target-nums[i]寻找下表,但是用建立哈希表替代遍历。 u...
    AnthonyM08阅读 164评论 0 0
  • 一、C语言基础 1、struct 的内存对齐和填充问题其实只要记住一个概念和三个原则就可以了: 一个概念:自然对齐...
    XDgbh阅读 2,350评论 1 38

友情链接更多精彩内容