d**t 发帖数: 183 | 1 vector name;
vector age;
How do I remove all the persons old than 30 from both name and age? I can do
it in a loop, but I am wondering if there is a more elegant way.
Thanks, |
t****t 发帖数: 6806 | 2 struct Person { string name; int age; };
vector person;
do
【在 d**t 的大作中提到】 : vector name; : vector age; : How do I remove all the persons old than 30 from both name and age? I can do : it in a loop, but I am wondering if there is a more elegant way. : Thanks,
|
d**t 发帖数: 183 | 3 Very nice. I will try it. Thanks.
Just curious. Is this the only way without using a loop?
【在 t****t 的大作中提到】 : struct Person { string name; int age; }; : vector person; : : do
|
i*****f 发帖数: 578 | 4 you still need to use a loop this way
【在 d**t 的大作中提到】 : Very nice. I will try it. Thanks. : Just curious. Is this the only way without using a loop?
|
d**t 发帖数: 183 | 5 I am thinking about using remove_if from the stl. Am I wrong?
【在 i*****f 的大作中提到】 : you still need to use a loop this way
|
t****t 发帖数: 6806 | 6 yes, you can use remove_if. but let me remind you that remove_if only "
compress" the range. you still have to remove the extra elements with erase(
) by yourself.
person.erase(remove_if(person.begin(), person.end(), your_predicate), person
.end());
【在 d**t 的大作中提到】 : I am thinking about using remove_if from the stl. Am I wrong?
|
d**t 发帖数: 183 | 7 Many thanks.
erase(
person
【在 t****t 的大作中提到】 : yes, you can use remove_if. but let me remind you that remove_if only " : compress" the range. you still have to remove the extra elements with erase( : ) by yourself. : person.erase(remove_if(person.begin(), person.end(), your_predicate), person : .end());
|
r*********r 发帖数: 3195 | |