C++ 中的类型推断是什么?
类型推断或类型检测是指在编程语言中自动检测表达式的数据类型的过程。该功能在一些强静态类型语言中具有。在 C++ 中,auto 关键字(在 C++ 11 中添加)用于自动类型推断。例如,如果你想创建一个迭代器来迭代一个向量,你可以简单地使用 auto 来实现此目的。
示例
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<int> arr(10);
for(auto it = arr.begin(); it != arr.end(); it ++) {
cin >> *it;
}
return 0;
}在上面的程序中,它将自动获得类型 std::vector<int>::iterator。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP