How to erase value from vector of struct using erase-remove idiom?

  • A+

How to erase all values from vector of struct, where struct value k equals to 0?

struct tabuRecord { int x; int y; int k; tabuRecord( int x, int y, int k)     : x(x), y(y), k(k){} };    vector <tabuRecord> tabu;  v.insert(v.begin(), tabuRecord(1, 2, 3)); v.insert(v.begin(), tabuRecord(4, 5, 0)); v.insert(v.begin(), tabuRecord(7, 8, 9)); v.insert(v.begin(), tabuRecord(10, 11, 0)); 

I have tried to

tabu.erase(std::remove(tabu.begin(), tabu.end(), tabu.k=0), tabu.end()); 


tabu.erase(std::remove(tabu.begin(), tabu.end(), tabuRecord.k=0), tabu.end()); 


I guess what you want to do is to remove all objects that have k==0, so create a lambda for that:

tabu.erase(     std::remove_if(tabu.begin(), tabu.end(),[](const tabuRecord& t){return t.k == 0;}),     tabu.end()); 

std::remove cannot work because it's not one value that you want to remove, but all values with a specific pattern, which is what std::remove_if does.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: