Skip to content

Instantly share code, notes, and snippets.

@cporter
Created November 26, 2012 06:46

Revisions

  1. Corey Porter revised this gist Nov 26, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions remove_if.cpp
    Original 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; });
  2. Corey Porter created this gist Nov 26, 2012.
    31 changes: 31 additions & 0 deletions remove_if.cpp
    Original 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;
    }