在模板中使用 vector::erase 时,将 const 迭代器作为“this”参数传递会丢弃限定符

问题描述

我正在尝试删除模板类型名称向量的第一个值。使用 vector stl 中的 erase() 方法,我不断收到此错误

TTrie.inc:56:19: error: passing ‘const std::vector<char,std::allocator<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
   56 |     sequence.erase(it);
      |     ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,from TTrie.h:5,from TTrieTest.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1430:7: note:   in call to ‘std::vector<_Tp,_Alloc>::iterator std::vector<_Tp,_Alloc>::erase(std::vector<_Tp,_Alloc>::const_iterator) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp,_Alloc>::iterator = std::vector<char,std::allocator<char> >::iterator; std::vector<_Tp,_Alloc>::const_iterator = std::vector<char,std::allocator<char> >::const_iterator]’
 1430 |       erase(const_iterator __position)
      |       ^~~~~
In file included from TTrie.h:115,from TTrieTest.cpp:1:
TTrie.inc: In instantiation of ‘TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>&) [with DataType = std::__cxx11::basic_string<char>]’:
TTrieTest.cpp:93:10:   required from here
TTrie.inc:56:19: error: passing ‘const std::vector<std::__cxx11::basic_string<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
   56 |     sequence.erase(it);
      |     ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,_Alloc>::const_iterator) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp,_Alloc>::iterator = std::vector<std::__cxx11::basic_string<char> >::iterator; std::vector<_Tp,_Alloc>::const_iterator = std::vector<std::__cxx11::basic_string<char> >::const_iterator]’
 1430 |       erase(const_iterator __position)
      |       ^~~~~
In file included from TTrie.h:115,from TTrieTest.cpp:1:

是否有问题,因为序列应该是常量?我是否以某种方式声明迭代器错误?这是我的代码。如果您有任何问题,请告诉我:

template<typename DataType>
TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>& sequence) {
  const DataType c = sequence[0];
  const TTrie<DataType>* child = getChild(c); //Returns a pointer to a child node or a nullptr

  if (!child) {
    TTrie<DataType>* n = new TTrie<DataType>(c);
    edgeMap[c] = n;
  }

  if(sequence.size() > 1) {
    typename std::vector<DataType>::const_iterator it;
    it = sequence.cbegin(); //First value of sequence
    sequence.erase(it);
    *edgeMap[c] += sequence;
  }
  return *this;
}

解决方法

您的 <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8'> <meta name="viewport" content="width=device-width,initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous"> <style></style> </head> <body> <table class="table table-borderless outer-table"> <thead> <tr> <th scope="col">ABC</th> <th scope="col">ABCD</th> </tr> </thead> <tbody> <tr> <td rowspan='2'> <figure class="figure"> <img class="figure-img img-fluid rounded" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22400%22%20height%3D%22300%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20400%20300%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_179a5a2c649%20text%20%7B%20fill%3Argba(255%2C255%2C255%2C.75)%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A20pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_179a5a2c649%22%3E%3Crect%20width%3D%22400%22%20height%3D%22300%22%20fill%3D%22%23777%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22148.84375%22%20y%3D%22158.8828125%22%3E400x300%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" data-holder-rendered="true"> <figcaption class="figure-caption">A caption for the above image</figcaption> </figure> </td> <td class="in">Lorem - Ipsum</td> </tr> <tr> <td class="out">Lorem - Ipsum</td> </tr> </tbody> </table> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> </body> </html> 参数是对 sequence 对象的引用。但是 std::vector::erase() 没有被限定为 const std::vector<DataType> 方法,因为它修改了 const 的内容,所以它不能在 vector 对象上调用。这就是错误消息试图告诉您的内容 - 正在对 const 对象调用 erase()