简体   繁体   English

使用迭代器擦除向量中的元素

[英]using iterator to erase element in vector

I have the following vector to store elements of type player: 我有以下向量来存储类型播放器的元素:

std::vector<player> players;

in a class called game which has the following function: 在一个叫做游戏的类中,它具有以下功能:

void game::removePlayer(string name) {
  vector<player>::iterator begin = players.begin();

  // find the player
  while (begin != players.end()) {
      if (begin->getName() == name) {
          break;
      }
      ++begin;
  }

  if (begin != players.end())
      players.erase(begin);

} }

I get the following error: 我收到以下错误:

    1>------ Build started: Project: texas holdem, Configuration: Debug Win32 ------
1>  game.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2514): error C2582: 'operator =' function is unavailable in 'player'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2535) : see reference to function template instantiation '_OutIt std::_Move<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_Nonscalar_ptr_iterator_tag)' being compiled
1>          with
1>          [
1>              _OutIt=player *,
1>              _InIt=player *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1170) : see reference to function template instantiation '_OutIt std::_Move<player*,player*>(_InIt,_InIt,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=player *,
1>              _InIt=player *
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1165) : while compiling class template member function 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<player,std::allocator<player>>,
1>              _Ty=player
1>          ]
1>          c:\vcprojects\texas holdem\texas holdem\game.h(29) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=player
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

removing the line 删除该行

players.erase(begin);

fixes the error, why is it happening though and how can I fix it? 修复错误,为什么会发生错误,我该如何解决?

You need to overload the assignment operator Player & operator= (const Player & other) for your class Player. 您需要为您的类Player重载赋值运算符Player & operator= (const Player & other) This required because erase requires it's argument to be copyable (it needs to rearrange the other elements of the vector after removal). 这是必需的,因为erase要求它的参数是可复制的(它需要在移除后重新排列向量的其他元素)。

What's happening is that the library code removes the player object by shoving each of the array elements above that iterator down one slot. 发生的事情是库代码通过将每个数组元素推到迭代器下面的一个插槽中来移除player对象。 To do that, it copies each object, using operator=. 为此,它使用operator =复制每个对象。 Apparently the class player doesn't have that operator. 显然,班级player没有那个操作员。

The problem is that your Player class is not movable. 问题是你的Player类不可移动。 In order to remove a Player from the middle of the vector, all the Player s after that have to be moved down one space in the vector. 为了从向量的中间移除Player ,之后的所有Player必须向下移动向量中的一个空格。 One solution is not to use a vector. 一种解决方案是不使用矢量。 Another is to make Player movable. 另一个是让Player移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM