auto item = val1 + val2; // 由val1和val2相加的结果推断出item的类型 auto i=0, *p = &i; // i是整数,p是整型指针
auto sz = 0, pi = 3.14; // Error!
int count = 10; int& countRef = count; auto myAuto = countRef; countRef = 11; cout << count << " "; // print 11 myAuto = 12; cout << count << endl; // print 11
int i = 0; const int ci = i, &cr = ci; auto b = ci; // b 是一个整数(ci的顶层const特性被忽略掉) auto c = cr; // c 是一个整数(cr是ci的别名,ci本身是一个顶层const) auto d = &i; // d 是一个整型指针(整数的地址就是指向整数的指针) auto e = &ci; // e 是一个指向整数常量的指针(对常量对象取地址是一种底层const)
const auto f = ci; // ci 的推演类型是int,f是const int类型
auto &g = ci; // g是一个整型常量引用,绑定到ci auto &h = 42; // Error: 不能为非常量引用绑定字面值 const auto &j = 42; // OK: 可以为常量引用绑定字面值
auto k = ci, &l = i; // k是整数,l是整型引用 auto &m = ci, *p = &ci; // m是对整型常量的引用,p是指向整型常量的指针 auto &n = i, *p2 = &ci; // Error: i的类型是int,而&ci的类型是const int
int j = 0; // Variable j is explicitly type int. auto k = 0; // Variable k is implicitly type int because 0 is an integer.
map<int,list<string>>::iterator i = m.begin(); auto i = m.begin();
#include <deque>
using namespace std;
int main()
{
deque<double> dqDoubleData(10, 0.1);
for (auto iter = dqDoubleData.begin(); iter != dqDoubleData.end(); ++iter)
{ /* ... */ }
// prefer range-for loops with the following information in mind
// (this applies to any range-for with auto, not just deque)
for (auto elem : dqDoubleData) // COPIES elements, not much better than the previous examples
{ /* ... */ }
for (auto& elem : dqDoubleData) // observes and/or modifies elements IN-PLACE
{ /* ... */ }
for (const auto& elem : dqDoubleData) // observes elements IN-PLACE
{ /* ... */ }
}
double x = 12.34; auto *y = new auto(x), **z = new auto(&x);
auto x = 1, *y = &x, **z = &y; // Resolves to int. auto a(2.01), *b (&a); // Resolves to double. auto c = 'a', *d(&c); // Resolves to char. auto m = 1, &n = m; // Resolves to int.
int v1 = 100, v2 = 200; auto x = v1 > v2 ? v1 : v2;
int f(int x) { return x; }
int main()
{
auto x = f(0);
const auto & y = f(1);
int (*p)(int x);
p = f;
auto fp = p;
//...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有