Tuesday, August 7, 2018

std::remove_if

#include <iostream>
#include <vector>
#include <algorithm>

int main(int argcv, char** argv)
{
    std::vector<int> test = {-1, 2, -3,-1,-5, 5,-5, -7,  -3, -3 , 6,  -1, 9 ,};
#if 1
    // success case
    auto f = [](int i) { return i < 0; };
    auto new_end =std::remove_if(test.begin(), test.end(), f);
    test.erase(new_end, test.end());
#else
    // failure case
    for(auto it = test.begin(); it != test.end(); it++){
        int i = *it;
        if(i < 0)
            test.erase(it);
    }
#endif
    for(auto it = test.begin(); it != test.end(); it++){
        int i = *it;
        printf("%d,", i);
    }
    printf("\n");
    return 0;
}


No comments:

Post a Comment