2015-01-10 15:26:06
神奇的C++0x 转载自CF Swift 的blog http://codeforces.com/blog/entry/15643
C++ Tricks
Last Updated: January 9, Added a trick about std::move
WARNING: Many of these things are belong to C++11 so use C++11 in order to test anything here :)
I just write a short version for this article, because it's now in the main page. I recommend you to click on "Read more »" and read more :) Here is a short trick for the short version:
I see lots of programmers write code like this one:
pair<int, int> p; vector<int> v; // ... p = make_pair(3, 4); v.push_back(4); v.push_back(5);
while you can just do this:
pair<int, int> p; // ... p = {3, 4}; v = {4, 5};
1. Assign value by a pair of {} to a container
I see lots of programmers write code like this one:
pair<int, int> p; // ... p = make_pair(3, 4);
while you can just do this:
pair<int, int> p; // ... p = {3, 4};
even a more complex pair
pair<int, pair<char, long long> > p; // ... p = {3, {'a', 8ll}};
What about vector
, deque
, set
and other containers?
vector<int> v; v = {1, 2, 5, 2}; for (auto i: v) cout << i << ' '; cout << '\n'; // prints "1 2 5 2"
deque<vector<pair<int, int>>> d; d = {{{3, 4}, {5, 6}}, {{1, 2}, {3, 4}}}; for (auto i: d) { for (auto j: i) cout << j.first << ' ' << j.second << '\n'; cout << "-\n"; } // prints "3 4 // 5 6 // - // 1 2 // 3 4 // -"
set<int> s; s = {4, 6, 2, 7, 4}; for (auto i: s) cout << i << ' '; cout << '\n'; // prints "2 4 6 7"
list<int> l; l = {5, 6, 9, 1}; for (auto i: l) cout << i << ' '; cout << '\n'; // prints "5 6 9 1"
array<int, 4> a; a = {5, 8, 9, 2}; for (auto i: a) cout << i << ' '; cout << '\n'; // prints "5 8 9 2"
tuple<int, int, char> t; t = {3, 4, 'f'}; cout << get<2>(t) << '\n';