#include<iostream> // using declaration; when we use the name cin, we get the one from the namespace std return0; }
usingstd::cin; intmain() { int i; cin >> i; // ok: cin is a synonym for std::cin cout << i; // error: no using declaration; we must use the full name std::cout << i; // ok: explicitly use cout from namepsace std
stringtemp(10, 'c'); // temp is cccccccccc string s8 = temp; // copy temp into s8
读写字符串
1 2 3 4 5 6 7
// Note: #include and using declarations must be added to compile this code int main() { string s; // empty string cin >> s; // read a whitespace-separated string into s cout << s << endl; // write s to the output return0; }
获取字符串的长度
1
auto len = line.size(); // len has type string::size_type
decltype(f()) sum = x; // sum has whatever type f returns.
1 2 3 4
constint ci = 0, &cj = ci; decltype(ci) x = 0; // x has type const int decltype(cj) y = x; // y has type const int& and is bound to x decltype(cj) z; // error: z is a reference and must be initialized
1 2 3 4
// decltype of an expression can be a reference type int i = 42, *p = &i, &r = i; decltype(r + 0) b; // ok: addition yields an int; b is an (uninitialized) int decltype(*p) c; // error: c is int& and must be initialized
decltype(*p)代表引用类型
decltype(()):仅代表引用类型
1 2
int i; decltype((i)) d; // error: d is int& and must be initialized
Exercises Section 2.5.3 Exercise 2.36: In the following code, determine the type of each variable and the value each variable has when the code finishes:
int a = 3, b = 4;
decltype(a) c = a;
decltype((b)) d = a;
++c;
++d;
答:c : int, d int &
Exercise 2.37:Assignment is an example of an expression that yields a reference type. The type is a reference to the type of the left-hand operand. That is, if i is an int, then the type of the expression i = x is int&. Using that knowledge, determine the type and value of each variable in this code:
int a = 3, b = 4;
decltype(a) c = a;
decltype(a = b) d = a;
答:c : int ; d : int &
Exercise 2.38: Describe the differences in type deduction between decltype and auto. Give an example of an expression where auto and decltype will deduce the same type and an example where they will deduce differing types.
typedefdouble wages; // wages is a synonym for double typedef wages base, *p; // base is a synonym for double, p for double*
wages i = 1.0; p val = &i;
写法二
1
using SI = int; // SI is a synonym for int
C++11支持
1 2 3
typedefchar *pstring; const pstring cstr = 0; // cstr is a constant pointer to char const pstring *ps; // ps is a pointer to a constant pointer to char
auto类型
编译器会自动通过初试值来判断auto对象的类型
1 2
autoi=0,*p=&i; //ok: i is int and p isapointerto intauto sz = 0, pi = 3.14; // error: inconsistent types for sz and pi
auto会自动忽略top-level const,保留low-level const
top-level const即自身为常量
low-level const即自身可以修改,但是所指向(引用)的地址储存值为常量
1 2 3 4
constint ci = i, &cr = ci; auto b = ci; // b is an int (top-level const in ci is dropped) auto c = cr; // c is an int (cr is an alias for ci whose const is top-level) autod=&i; // d isan int*(& ofan int objectis int*) auto e = &ci; // e is const int*(& of a const object is low-level const)
设置为top-level const
1
constauto f = ci; // deduced type of ci is int; f has type const int