C++中auto的使用

auto 是 C++ 里非常重要、刷题和工程都会大量用到的关键字

一、auto 是什么?

auto 让编译器根据初始化表达式,自动推导变量的类型。

你不用手写类型,编译器帮你"算出来"。


二、最基础的用法

1️⃣ 变量定义

cpp 复制代码
auto x = 10;        // int
auto y = 3.14;      // double
auto s = string("a"); // string

等价于你手写:

cpp 复制代码
int x = 10;
double y = 3.14;
string s = "a";

⚠️ 必须初始化,否则无法推导:

cpp 复制代码
auto a;   // ❌ 错

三、auto + 引用 / const(非常重要)

2️⃣ auto& ------ 引用,避免拷贝

cpp 复制代码
vector<int> v{1,2,3};
for (auto& x : v) {
    x++;     // 修改原数组
}
  • xint&
  • 不拷贝,能修改

3️⃣ const auto& ------ 只读引用(最常用)

cpp 复制代码
for (const auto& x : v) {
    cout << x;
}
  • 不拷贝
  • 不能修改
  • 遍历容器的首选写法

4️⃣ auto(无 &)------ 会拷贝

cpp 复制代码
for (auto x : v) {
    x++;     // 只改拷贝,不改 v
}

四、auto 在结构化绑定中的用法(C++17)

cpp 复制代码
pair<int,int> p = {1,2};
auto [a, b] = p;

等价于:

cpp 复制代码
int a = p.first;
int b = p.second;

⚠️ 结构化绑定只能用 auto,不能写具体类型。

引用版结构化绑定(常用)

cpp 复制代码
auto& [a, b] = p;          // 可修改 p
const auto& [a, b] = p;   // 只读

五、auto 在函数返回值中的用法

1️⃣ 接收返回值

cpp 复制代码
auto res = dfs(root);  // res 类型由 dfs 返回值决定

2️⃣ 返回类型推导(C++14)

cpp 复制代码
auto add(int a, int b) {
    return a + b;  // 返回 int
}

六、auto 在 STL / 刷题中的典型场景

1️⃣ 遍历容器(99% 推荐)

cpp 复制代码
for (const auto& it : container) { ... }

2️⃣ 接收复杂类型

cpp 复制代码
unordered_map<string, vector<pair<int,double>>> mp;
for (const auto& [key, vec] : mp) { ... }

3️⃣ DFS / 树 DP 返回 pair

cpp 复制代码
auto [take, skip] = dfs(node);

七、auto 的推导规则(知道这 3 条就够)

1️⃣ auto 会丢掉顶层 const

cpp 复制代码
const int x = 10;
auto y = x;        // y 是 int

2️⃣ auto& 会保留引用和 const

cpp 复制代码
const int x = 10;
auto& y = x;       // y 是 const int&

3️⃣ 初始化表达式决定一切

cpp 复制代码
auto a = {1,2,3};  // initializer_list<int>

八、常见坑(一定要避开)

❌ 误以为 auto 不会拷贝

cpp 复制代码
for (auto x : bigVector) { ... } // 每次拷贝

改为:

cpp 复制代码
for (const auto& x : bigVector) { ... }

❌ 结构化绑定写成具体类型

cpp 复制代码
pair<int,int> [a,b] = p;  // ❌

只能:

cpp 复制代码
auto [a,b] = p;           // ✅

❌ 忽略 auto 推导导致类型变化

cpp 复制代码
auto x = 0;    // int
x = 3.14;      // 3,被截断

九、什么时候"应该"用 auto?

✅ 强烈推荐用 auto 的情况

  • 类型很长(STL 容器)
  • 遍历容器
  • 接收函数返回值
  • 结构化绑定

⚠️ 不建议用 auto 的情况

  • 类型对可读性很关键(如 int / double 混用)
  • API 对外接口(头文件)

十、刷题级使用口诀

遍历容器用 const auto&

要改元素用 auto&

拿返回值用 auto

解构 pair/tupleauto [a,b]


十一、一句话面试总结

auto 是编译期类型推导工具

用来减少冗余、避免拷贝、提升可读性

但要配合 & / const 使用,避免隐式性能问题

相关推荐
大王小生4 分钟前
C# CancellationToken
开发语言·c#·token·cancellation
listhi5205 分钟前
基于C#实现屏幕放大镜功能
开发语言·c#
我叫袁小陌30 分钟前
C++多线程全面详解
开发语言·c++
wen__xvn39 分钟前
代码随想录算法训练营DAY14第六章 二叉树 part02
数据结构·算法·leetcode
lihongli00041 分钟前
【工程实战】Win11 + Ubuntu20.04 + Ubuntu24.04 三系统长期稳定安装方案(含避坑指南)
开发语言
Ka1Yan42 分钟前
[数组] - 代码随想录(2-6)
数据结构·算法·leetcode
黄宝康1 小时前
sublimetext 运行python程序
开发语言·python
m0_748250031 小时前
C++ 官方文档与标准
开发语言·c++
zh_xuan1 小时前
kotlin 类继承的语法2
开发语言·kotlin
漫随流水1 小时前
leetcode算法(104.二叉树的最大深度)
数据结构·算法·leetcode·二叉树