C++ Tricks 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;
vector<int> v;
// ...
p = {3, 4};
v = {4, 5};
~~~~~
[cut]
[This](http://codeforces.me/blog/entry/10124) is a great C++11 tutorial for those who want to know more about C++11.
#### 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<in...
construct value at the end of vector but `push_back` construct it somewhere
else and then move it to the, I see lots of programmers write code like this one: ~~~~~ pair p; vector v, This library includes many of libraries we do need in contest like `algorithm`,
`iostream`, `vector, What about `vector`, `deque`, `set` and other containers?, When you work with STL containers like `vector`, you can use `move` function to
just move container, deque<vector>> d; d = {{{3, 4}, {5, 6}}, {{1, 2}, {3, 4}}}; for (auto i: d, vector v; v = {1, 2, 5, 2}; for (auto i: v) cout << i << ' '; cout << '\n'; //
prints, vector> v; v.eb(a,b); // shorter and faster than pb(mp(a,b)), ~~~~~ pair p; vector v; // ... p = make_pair(3, 4); v.push_back(4); v.push_back, ~~~~~ pair p; vector v; // ... p = {3, 4}; v = {4, 5}; ~~~~~, ~~~~~ vector v = {1, 2, 3, 4}; vector w = move(v);, ~~~~~ vector v = {3, 1, 2, 1, 8}; sort(begin(v), end(v), [] (int a, int b) {
return a > b, ~~~~~ vector v = {4, 5, 6, 4, 8}; rep(it, end(v), begin(v)) cout << *it, ~~~~~ vector v = {8, 2, 3, 1}; for (auto &it: v) it *= 2; for (auto it: v) cout
Full text and comments »