Erase-remove idiom in C++
Level 4194 keystrokes
Removes every 2 from a vector with the erase-remove idiom: remove moves the kept elements forward and erase cuts off the rest, leaving 1 3 4 5.
C++ code
1vector<int> nums = {1, 2, 3, 2, 4, 2, 5};// remove shifts the non-2 elements to the front and returns the new logical end; the size does not change yet2auto it = remove(nums.begin(), nums.end(), 2);// erase actually deletes the leftover elements from it to the end3nums.erase(it, nums.end());// Three 2s were removed from seven elements: prints Size: 44cout << "Size: " << nums.size() << endl;// The remaining order is kept: prints 1 3 4 55for (int n : nums) cout << n << " ";Output
Size: 4 1 3 4 5
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
<× 11,× 9(× 7)× 7;× 5.× 5"× 4:× 2=× 2{× 1