汽车的含义:
Meaning of auto& :
本文关键字:汽车 更新时间:2023-10-16
我知道auto意味着类型扣除。 我从未见过它用作auto&
,此外我不明白:
在这个简短的代码中做了什么。
#include <iostream>
#include <vector>
#include <thread>
void PrintMe() {
std::cout << "Hello from thread: " << std::this_thread::get_id() << std::endl;
}
int main() {
std::vector<std::thread> threads;
for(unsigned int i = 0; i < 5; i++) {
threads.push_back(std::thread(PrintMe));
}
for(auto& thread : threads) {
thread.join();
}
return 0;
}
我可以猜到这是某种替代合成糖的
for(std::vector<std::thread>::iterator it = threads.begin(); it != threads.end(); it++ ) {
(*it).join();
}
但我不明白这种语法是如何工作的,以及那个和符号在那里做什么。
您的示例代码几乎是正确的。
自动含义在第 11 C++ 中重新定义。编译器将推断正在使用的变量的正确类型。
语法:
它是基于范围。这意味着循环将解析线程向量中的每个元素。
在 for 中,您需要指定别名auto&
以避免在 thread
变量中的向量内创建元素的副本。这样,对 thread
var 执行的每个操作都是在 threads
向量内的元素上完成的。此外,在基于范围的 for 中,出于性能原因,您始终希望使用引用&
。
相关文章:
- 我可以从标准::访问返回汽车吗?
- 汽车仪表模拟器
- 范围TS成语和神秘的汽车&&
- 如何让 C++11 汽车更智能?
- 2WD蓝牙控制的汽车的问题无法停止
- 汽车的检测和计数 opencv3.0.0 c++.
- 如何使用字符串和整数获取汽车类的整数结果
- 使用C++类计算汽车移动和距离
- 从基类(车辆)继承方法,从派生类(汽车)继承值,以使用 C++ 在另一个派生类(车道)中实现
- 马可怪物的汽车物理演示改编C++,汽车行为混乱
- 计算汽车节点静脉之间的距离
- 自动驾驶汽车 Android "opencv.dir"未在本地媒体资源中设置 - Android Studio
- 这对汽车的用途是什么? - ADL
- 汽车的定制仪表板;使用C 读取OBD II数据
- 同时使用汽车和汽车修理工类,似乎无法更改对象的成员?
- 为什么是如果(类型汽车!= 'X' ||"S" ||'L') 的评估值始终为真
- 带有超声波传感器(避障)代码的手势控制汽车无法正常工作
- 汽车贷款计算(C++)
- 创建一个动态数组,包括汽车的年份和品牌,但无法保存我的第一个输出
- C++汽车计划