I have ended up to following code in order to filter out some bad elements from an std::vector:
#include <iostream>
#include <vector>
#include <algorithm>
typedef struct mystruct {
int id;
std::string name;
};
int main()
{
std::vector<mystruct> all_items = {{151, "test1"}, {154, "test4"}, {152, "test2"}, {151, "test1"}, {151, "test1"}, {153, "test3"}};
std::vector<int> bad_ids = {151, 152};
std::vector<mystruct> filter_items;
for (const auto& item : all_items) {
if ( std::find(bad_ids.begin(), bad_ids.end(), item.id) != bad_ids.end() ) {
std::cout << "id: " << item.id << " is bad" << std::endl;
} else {
std::cout << "id: " << item.id << " is good item" << std::endl;
filter_items.emplace_back(item);
}
}
for (auto f : filter_items) {
std::cout << "Good item: " << f.id << std::endl;
}
}
Is there any more efficient way? Can the std::remove_copy_if or Boost be used here and how?