一、以auto完成類型自動推導
1、auto的使用場景
- 在C++11中允許聲明一個變量或者對象(object)而不需要指明其類型,只需要使用auto
?auto i = 42; //i has type int
int x = 520;
?auto p = &x;
?auto* p = &x;
?聲明引用類型時要加&
?auto& q = x;
double f = 12.3;
?auto j = f; //j has type double
- 限定符對
auto
也同樣適用
?static auto i = 250
- 在編程時使用auto在一些語句中可以大量的縮減代碼,最典型的應用就是對迭代器的聲明,例如:
vector<int> ans;
auto it = ans.begin();
//is equl to
vector<int>::iterator it = ans.begin();
- 在定義模板函數時,用于聲明依賴模板參數的變量類型以及定義模板函數依賴于模板參數的返回值
template <typename _Tx,typename _Ty>
void Multiply(_Tx x, _Ty y)
{
auto v = x*y;
std::cout << v;
}
template <typename _Tx, typename _Ty>
auto multiply(_Tx x, _Ty y)->decltype(x*y)//decltype操作符用于查詢表達式的數據類型
{
return x*y;
}
//auto在這里的作用也稱為返回值占位,它只是為函數返回值占了一個位置,真正的返回值是后面的decltype(_Tx*_Ty)
2、auto的注意事項
- 使用auto可以自動推導出變量的類型,因此初始化操作是必不可少的
?auto i; i沒有對象對其進行初始化
- auto不能用于聲明函數的參數
?void func(auto i){}
- auto聲明數組時要注意
int a[3] = {1,2,3};
auto b = a // type b is int*
auto& c = a // type c is int[3]
- auto連續聲明變量是,變量類型要一致
?auto a = 11,b = 22;
?auto a = 11,b = 2,2; //類型不一致,無法推導為同一類型
- auto聲明const類型變量和引用變量時,注意對語義的去除
const int a = 1;
auto b = a; //此時b類型為int
?const auto c = a; //此時c類型為const int
int a = 1;
int &b = a;
auto c = b; //此時c類型為int c = 10;a = 1;
auto &d = b; //此時d類型為int& d = 10;a = 10;
二、Range-Based for 循環
1、range-based for循環的使用
Range-Based for 循環是在C++11 中引入的一種新的循環形式,它可以逐一迭代某個區間、數組、集合中的每一個元素,和其他語言中for range循環比較類似,其基本形式為:
for(decl : coll){
statement;
}
其中decl
是給定的coll
集合中的每一個元素的聲明,針對這些元素會執行給定的statement
,例如:
int a[3] = {1,2,3};
for(int i : a){
cout<<i;
}
/*輸出
123
*/
一般來說如果coll
提供成員函數begin()
和end()
,一個range-based for循環等同于:
for(auto it = coll.begin();it != coll.end();++it){
decl = *it;
statement;
}
加上auto
后可以十分方便的在如下場景使用:
- 遍歷stl容器
vector<int> a(10,1);
for(auto i : a){
cout<<i<<endl;
}
int arr[3] = {1,2,3};
for(auto i : arr){
cout<<i<<endl;
}
2、range-based for循環的注意事項
- 如果要改變遍歷對象的值應聲明為引用型:
string s = "AAA";
for(auto i : a){
i = 'B';
}
cout<<s;
/*AAA*/
for(auto &j : s){
j = 'B';
}
cout<<s;
/*BBB*/
- 對map的遍歷:
map<int,int> a = { { "a", 1 }, { "b", 2 }, { "c", 3 } };
for(auto &it : a){
cout<<it.first<<" "<<it->second<<endl;
}
/*
a 1
b 2
c 3
*/
我們仍然要加上.first/.second
因為auto自動推導的是容器的類型,而不是迭代器類型,map
的value_type
是std::pair
因此還需要val.first/val.second
來訪問數據
- 當元素在for循環中被初始化為
decl
,不得有任何顯式類型轉換(explicit type conversion)
class C{
public:
explicit C(const string& s);
...
};
vector<string> vs;
for(const C& elem : vs){ //ERROR,no conversion from string to C defined
cout<<elem<<endl;
}
在編程中使用這樣的方式,可以實實在在的提升幸福感,也提高了碼代碼的速度