erase-remove-idiom

Where is the performance gain of the erase-remove idiom coming from

南笙酒味 提交于 2021-02-19 03:15:40
问题 I need to erase all elements from a vector which fulfill a certain criteria. My first approach would be to loop through the vector and call vector::erase on all elements which fulfill the criteria. As far as I understand, vector::erase has a bad performance for this use case, because it removes the item from the underlying array, and moves the rest of the vector forward by one element (or more if you erase a range of elements). When you remove multiple elements, the rear elements will be

C++ nested for loop with erasing elements

荒凉一梦 提交于 2021-02-11 14:14:20
问题 I would like to check all Elements of an vector against each other. By checking a condition an element should be removed. One approach was to erase the elements by nested for loops for (int a = 0; a < rs.size(); a++) { Point A = rs[a]; for (int b = 1; b <= rs.size(); b++) { Point B = rs2[b]; float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0); if (distance < 10.0) { if (distance > 0) { rs.erase(rs.begin() + b); } } } } but this would effect the vector and his size in runtime. A

C++ nested for loop with erasing elements

你离开我真会死。 提交于 2021-02-11 14:10:27
问题 I would like to check all Elements of an vector against each other. By checking a condition an element should be removed. One approach was to erase the elements by nested for loops for (int a = 0; a < rs.size(); a++) { Point A = rs[a]; for (int b = 1; b <= rs.size(); b++) { Point B = rs2[b]; float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0); if (distance < 10.0) { if (distance > 0) { rs.erase(rs.begin() + b); } } } } but this would effect the vector and his size in runtime. A

Erase in a loop with a condition in C++

狂风中的少年 提交于 2021-01-27 12:46:32
问题 Is there a better way to write: for (auto i = container.begin(); i != container.end();) { if (condition(i)) { i = container.erase(i); continue; } ++i; } This code does what I want, but it feels like bad style. How can I improve it? My container is std::map , but a generic solution would be cool. 回答1: Use erase + remove_if : auto pred = /* lambda or something*/ container.erase(std::remove_if(container.begin(), container.end(), pred) 回答2: Is there a better way to...? It is always subjective,

STL “erase-remove” idiom: Why not “resize-remove”?

99封情书 提交于 2019-12-23 07:25:41
问题 It is commonly understood that a good way to fully delete desired items from a std::vector is the erase-remove idiom. As noted in the above link (as of the date of this posting), in code the erase-remove idiom looks like this: int main() { // initialises a vector that holds the numbers from 0-9. std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // erase-remove idiom to completely eliminate the desired items from the vector v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v

STL “erase-remove” idiom: Why not “resize-remove”?

一个人想着一个人 提交于 2019-12-23 07:24:53
问题 It is commonly understood that a good way to fully delete desired items from a std::vector is the erase-remove idiom. As noted in the above link (as of the date of this posting), in code the erase-remove idiom looks like this: int main() { // initialises a vector that holds the numbers from 0-9. std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // erase-remove idiom to completely eliminate the desired items from the vector v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v

In c++, is it safe to use std::numeric_limits<double>::max() as a special “flag”?

风流意气都作罢 提交于 2019-12-12 15:36:55
问题 Given std::vector<double> a; std::vector<int> ind; where ind is 1 sorted in ascending order. I want to do the equivalent of the following: for (auto it=ind.rbegin();it!=ind.rend();it++) a.erase(a.begin() + *it); I came up with this: for (auto it=ind.begin();it!=ind.end();it++) a[*it] = std::numeric_limits<double>::max(); std::erase(std::remove(a.begin(),a.end(),std::numeric_limits<double>::max()),a.end()); This is very fast, but it doesn't feel right to use the std::numeric_limits::max() as a

Why is a convenience helper for the erase-remove-idiom not provided by the standard?

廉价感情. 提交于 2019-12-12 11:40:50
问题 Removing items from a collection in the STL requires a technique used so often that is has become an idiom: the erase-remove-idiom One of the most common usages of this idiom is to remove an item of type T from a vector<T> std::vector<Widget> widget_collection; Widget widget; widget_collection.erase( std::remove(widget_collection.begin(), widget_collection.end(), widget), widget_collection.end()); This is obviously very verbose, and violates the DRY principle - the vector in question is

Remove vector elements based on the index

怎甘沉沦 提交于 2019-12-01 05:51:53
I wanted to remove the elements of the vector based on the index, say all the even indexed elements. I have read about the erase remove idiom but can't see how to apply it. This is what I tried: vector<int> line; line.reserve(10); for(int i=0;i<10;++i) { line.push_back(i+1); } for(unsigned int i=0;i<line.size();++i) { //remove the even indexed elements if(i%2 == 0) { remove(line.begin(),line.end(),line[i]); } } line.erase( line.begin(),line.end() ); This erases the entire vector. I was hoping to only remove the elements that had been marked by the remove algorithm. Then I tried this for

Remove vector elements based on the index

不想你离开。 提交于 2019-12-01 03:27:53
问题 I wanted to remove the elements of the vector based on the index, say all the even indexed elements. I have read about the erase remove idiom but can't see how to apply it. This is what I tried: vector<int> line; line.reserve(10); for(int i=0;i<10;++i) { line.push_back(i+1); } for(unsigned int i=0;i<line.size();++i) { //remove the even indexed elements if(i%2 == 0) { remove(line.begin(),line.end(),line[i]); } } line.erase( line.begin(),line.end() ); This erases the entire vector. I was hoping