Created
October 19, 2016 12:00
-
-
Save mstybird/38a8de05329fbaee3064927991c515f7 to your computer and use it in GitHub Desktop.
C++ Vector std::remove_if
This file contains 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 characters
#include<iostream> | |
#include<vector> | |
#include<algorithm> | |
using namespace std; | |
template<typename T> | |
void OutVector(vector<T>&aData) { | |
for (auto&x : aData) { | |
cout << x << ","; | |
} | |
cout << endl; | |
} | |
int main() { | |
vector<int> data = { | |
1,2,3,2,3,4,5,2,3 | |
}; | |
OutVector(data); | |
//値に2を持つ要素を削除 | |
auto removeIt = remove_if(data.begin(), data.end(), [&](int value) { | |
return value == 2; | |
} | |
); | |
//これで初めて要素が削除される | |
data.erase(removeIt, data.end()); | |
OutVector(data); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment