Created
November 26, 2012 06:46
Revisions
-
Corey Porter revised this gist
Nov 26, 2012 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,8 @@ int main (int, char **) { // won't remove for (auto &x : ints) { // The following doesn't change ints at all, even though it // hits one every element auto it = remove_if (ints . begin (), ints . end (), [](const int y) { return true; }); -
Corey Porter created this gist
Nov 26, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ // -*- compile-command: "clang++ -std=gnu++0x -stdlib=libc++ -o remove_if remove_if.cpp" -*- #include <iostream> #include <vector> int main (int, char **) { std::vector<int> ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // won't remove for (auto &x : ints) { auto it = remove_if (ints . begin (), ints . end (), [](const int y) { return true; }); std::cout << x << std::endl; } std::cout << ints . size () << " elements in ints" << std::endl; // will remove for (auto &x : ints) { auto it = remove_if (ints . begin (), ints . end (), [](const int y) { return true; }); ints . resize (it - ints . begin ()); // And yet this will happen ten times! std::cout << x << std::endl; } std::cout << ints . size () << " elements in ints" << std::endl; return 0; }