Missing const_iterator overload of std::vector::erase() with g++ 4.8

后端 未结 2 1419
情话喂你
情话喂你 2020-12-11 02:39

The following example will not compile using g++ 4.8.2:

#include 
#include 
using namespace std;

int main() {
    vector

        
相关标签:
2条回答
  • 2020-12-11 03:18

    This is a known bug in gcc: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57158

    erase requires an iterator instead of a const_iterator with current gcc's.

    0 讨论(0)
  • 2020-12-11 03:23

    You can get a non-const iterator via pointer arithmetic, here's a helper function to do that:

    template<typename T>
      typename std::vector<T>::iterator
      const_iterator_cast(std::vector<T>& v, typename std::vector<T>::const_iterator iter)
      {
        return v.begin() + (iter - v.cbegin());
      }
    

    used like so:

    std::vector<T> v(1);
    auto citer = v.cbegin();
    v.erase( const_iterator_cast(v, citer) );
    
    0 讨论(0)
提交回复
热议问题