Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.07 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Alternative for std:vector to remove its elements while going through a loop?
  2. for(unsigned int j = 0; j < rectArray.size(); j++)
  3. {
  4.  if( rectArray[j] == 2 )
  5.   {
  6.    rectArray.erase(rectArray.begin() + j);
  7.   }
  8. //...
  9. }
  10.        
  11. for (auto it = rectArray.begin(); it != rectArray.end(); ++it)
  12. {
  13.     // Access the current element with *it
  14.     // If you want you can pass `it` and `rectArray.end()` as
  15.     // the lower and upper bounds of the new collection,
  16.     // rather than doing expensive resizes of the vector.
  17. }
  18.        
  19. for (auto it : rectArray) {
  20.    // same as before
  21. }
  22.        
  23. auto end = std::end(rectArray);
  24. for(auto it = std::begin(rectArray); it != end; ++it)
  25. {
  26.     if(it->remove_me()))    
  27.         std::swap(*it, *--end); // or even faster *it = std::move(*--end);
  28. }    
  29. rectArray.erase(end, std::end(rectArray));
  30.        
  31. #include <vector>
  32. #include <list>
  33. #include <algorithm>
  34.  
  35. using namespace std;
  36.  
  37. class Widget
  38. {
  39. public:
  40.    explicit Widget(int someNumber);
  41.    bool ShouldDelete();
  42.    bool ShouldDeleteComplex(int a, int b, int c);
  43. private:
  44.    int _someNumber;
  45. };
  46.  
  47. Widget::Widget(int someNumber) : _someNumber(someNumber)
  48. {
  49. }
  50.  
  51. bool Widget::ShouldDelete()
  52. {
  53.    if (_someNumber > 2)
  54.    {
  55.       return true;
  56.    }
  57.    return false;
  58. }
  59.  
  60. bool Widget::ShouldDeleteComplex(int a, int b, int c)
  61. {
  62.    if ((a * b - c) > _someNumber)
  63.    {
  64.       return true;
  65.    }
  66.    return false;
  67. }
  68.  
  69. int main()
  70. {
  71.    list<Widget> lw;
  72.  
  73.    lw.push_back(Widget(1));
  74.    lw.push_back(Widget(2));
  75.    lw.push_back(Widget(3));
  76.  
  77.    // delete from list using functor
  78.    lw.remove_if(mem_fun_ref(&Widget::ShouldDelete));
  79.  
  80.    // delete from list using lambda function
  81.    lw.remove_if([] (Widget& x) { return x.ShouldDeleteComplex(1, 2, 0); }   );
  82.  
  83.    vector<Widget> vw;
  84.  
  85.    vw.push_back(Widget(1));
  86.    vw.push_back(Widget(2));
  87.    vw.push_back(Widget(3));
  88.  
  89.    // delete using functor
  90.    vw.erase(remove_if(vw.begin(), vw.end(), mem_fun_ref(&Widget::ShouldDelete)), vw.end());
  91.  
  92.    // delete using lambda function
  93.    vw.erase(
  94.       remove_if(vw.begin(), vw.end(),
  95.         [] (Widget& x) { return x.ShouldDeleteComplex(1, 2, 0); }
  96.      ),
  97.       vw.end());
  98.  
  99.    return 0;
  100. }