erase-remove-idiom

std::remove_if - lambda, not removing anything from the collection

断了今生、忘了曾经 提交于 2019-11-30 07:02:40
问题 Ok, I expect I've made a dumb mistake here. I have a list of DisplayDevice3d and each DisplayDevice3d contains a list of DisplayMode3d. I want to remove all items from the list of DisplayDevice3d that don't have any DisplayMode3d's. I'm trying to use a Lambda to do it, ie.: // If the device doesn't have any modes, remove it. std::remove_if(MyDisplayDevices.begin(), MyDisplayDevices.end(), [](DisplayDevice3d& device) { return device.Modes.size() == 0; } ); Even though out of 6 DisplayMode3d's

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

你说的曾经没有我的故事 提交于 2019-11-29 17:16:25
问题 We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom: one can easily get into the pitfall of typos like c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing here or c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase It even follows the wrong semantics for containers like std::list by

std::remove_if - lambda, not removing anything from the collection

两盒软妹~` 提交于 2019-11-29 00:54:38
Ok, I expect I've made a dumb mistake here. I have a list of DisplayDevice3d and each DisplayDevice3d contains a list of DisplayMode3d. I want to remove all items from the list of DisplayDevice3d that don't have any DisplayMode3d's. I'm trying to use a Lambda to do it, ie.: // If the device doesn't have any modes, remove it. std::remove_if(MyDisplayDevices.begin(), MyDisplayDevices.end(), [](DisplayDevice3d& device) { return device.Modes.size() == 0; } ); Even though out of 6 DisplayMode3d's in MyDisplayDevices, only 1 has any DisplayMode3d's in its Modes collection, nothing is being removed

erase() after performing remove_if()

你说的曾经没有我的故事 提交于 2019-11-28 13:20:20
I've created a function to run through a vector of strings and remove any strings of length 3 or less. This is a lesson in using the STL Algorithm library. I'm having trouble in that the functions work but not only does it delete strings of length 3 or less but it also appends the string "vector" to the end. The output should be This test vector and instead it is This test vector vector" How can I fix it? /* * using remove_if and custom call back function, write RemoveShortWords * that accepts a vector<string> and removes all strings of length 3 or * less from it. *shoot for 2 lines of code in

Is there a better alternative to std::remove_if to remove elements from a vector?

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:58:31
The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly? However the standard way of doing this in C++ seems to be the following idiom: std::vector<int> ints; ... ints.erase( std::remove_if(ints.begin(), ints.end(), [](int x){return x < 0;}), ints.end()); This example removes all elements less than zero from an integer vector. I find it not only ugly but also easy to use incorrectly. It is clear that std::remove_if cannot change the

Using erase-remove_if idiom

梦想与她 提交于 2019-11-28 00:08:26
Let's say I have std::vector<std::pair<int,Direction>> . I am trying to use erase-remove_if idiom to remove pairs from the vector. stopPoints.erase(std::remove_if(stopPoints.begin(), stopPoints.end(), [&](const stopPointPair stopPoint)-> bool { return stopPoint.first == 4; })); I want to delete all pairs that have .first value set to 4. In my example I have pairs: - 4, Up - 4, Down - 2, Up - 6, Up However, after I execute erase-remove_if, I am left with: - 2, Up - 6, Up - 6, Up What am I doing wrong here? The correct code is: stopPoints.erase(std::remove_if(stopPoints.begin(), stopPoints.end()

Is there a better alternative to std::remove_if to remove elements from a vector?

假装没事ソ 提交于 2019-11-27 02:32:03
问题 The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly? However the standard way of doing this in C++ seems to be the following idiom: std::vector<int> ints; ... ints.erase( std::remove_if(ints.begin(), ints.end(), [](int x){return x < 0;}), ints.end()); This example removes all elements less than zero from an integer vector. I

Using erase-remove_if idiom

*爱你&永不变心* 提交于 2019-11-26 21:34:35
问题 Let's say I have std::vector<std::pair<int,Direction>> . I am trying to use erase-remove_if idiom to remove pairs from the vector. stopPoints.erase(std::remove_if(stopPoints.begin(), stopPoints.end(), [&](const stopPointPair stopPoint)-> bool { return stopPoint.first == 4; })); I want to delete all pairs that have .first value set to 4. In my example I have pairs: - 4, Up - 4, Down - 2, Up - 6, Up However, after I execute erase-remove_if, I am left with: - 2, Up - 6, Up - 6, Up What am I

C++ Erase vector element by value rather than by position? [duplicate]

两盒软妹~` 提交于 2019-11-26 12:00:00
This question already has an answer here: How do I remove an item from a stl vector with a certain value? 10 answers vector<int> myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of "8", I think I would do this: myVector.erase(myVector.begin()+4); Because that would erase the 4th element. But is there any way to erase an element based off of the value "8"? Like: myVector.eraseElementWhoseValueIs(8); Or do I simply just need to iterate through all the vector elements and test their values? Georg

C++ Erase vector element by value rather than by position? [duplicate]

筅森魡賤 提交于 2019-11-26 02:39:54
问题 This question already has answers here : How do I remove an item from a stl vector with a certain value? (10 answers) Closed 3 years ago . vector<int> myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of \"8\", I think I would do this: myVector.erase(myVector.begin()+4); Because that would erase the 4th element. But is there any way to erase an element based off of the value \"8\"? Like: myVector