prettify

Oct 17, 2016

C++ 11: array, auto, range based loop, enum

1. std::array is a static vector. The size of std::array can not be changed after it was compiled, thus is it often more efficient than std::vector.

std:array<int> nums={1,3,4,9};
for(auto i=nums.begin(); i!=nums.end(); ++i) cout << i << " ";

2. auto type and range based loop
std:array<int> nums={1,3,4,9};
for(auto& i: nums) cout << i << " ";

then range based loop almost can apply to any complicated type, e.g

map<int,string> m;
for(auto& x: m) cout<<x.first << ":" << x.second << endl;

pair<int,int> p;
for(auto& x: p) cout <<x.first <<":"<<x.second<<endl;

vector<int> a;
for(auto& x: a) cout << x << endl;

priority_queue<int> pq;
for(auto& x: pq) cout <<x<<endl;

3. enum class
enum class color {red, green, blue};
color c=color::green;


No comments:

Post a Comment