[英]C++ STL vector.erase() always deletes last element
Using: Eclipse 3.8.1, C/C++ Remote Debug Launcher 6.0.0, with GCC 1.1.0, and GDB 7.0.0 Verified on: Visual Studio 2010 使用:Eclipse 3.8.1,C / C ++远程调试启动器6.0.0,带有GCC 1.1.0和GDB 7.0.0的验证:Visual Studio 2010
I am pulling my hair out on this one. 我正在把头发拉出来。 When using the following code, the last element of the vector of Room objects rooms is always removed.
使用以下代码时,始终会移除Room对象room的向量的最后一个元素。 I cannot get any element other than the last to erase with this code.
除了最后一个,我无法使用此代码擦除任何元素。 I am only trying to remove one element (the location is determined by the user).
我只想删除一个元素(位置由用户确定)。
I know that many of you are probably asking why I don't use a list, but random data access is much more important than ease of element addition / removal with this collection. 我知道你们中的许多人可能会问为什么我不使用列表,但是随机数据访问比使用此集合轻松添加/删除元素更为重要。 Do you have any ideas?
你有什么想法?
void House::removeRoom(unsigned int roomToRemove){
try{
if(roomDoesNotExist(roomToRemove)) throw houseException("Room requested to remove does not exist", roomToRemove, __FILE__, __LINE__);
vector<Room>::iterator roomIterator = rooms.begin() + roomToRemove;
rooms.erase(roomIterator);
removeAllLinksToRoom(roomToRemove);
renumberLinkedRoomsAfterErase(roomToRemove);
}
catch(houseException& hException){
hException.display(cerr);
}
}
Here is a snippet of the House class: 这是众议院课程的片段:
#include "Room.h"
using namespace std;
class House {
public:
vector<Room> rooms;
//member functions
void removeRoom(unsigned int roomToRemove);
//constuctors / destructors
House();
virtual ~House();
//STL linked list overload operators =, ==, and <
House &operator=(const House &rhs);
int operator==(const House &rhs) const;
int operator<(const House &rhs) const;
private:
bool roomDoesNotExist(int roomToRemove);
void removeAllLinksToRoom(int roomToUnlink);
void renumberLinkedRoomsAfterErase(int erasedRoom);
};
Because it could be relevant, I included my entire Room class: 因为可能相关,所以我包括了整个“房间”课程:
#include "Wall.h"
#include <vector>
#include <algorithm>
#include <string>
#include "exceptions/houseException.h"
using namespace std;
class Room {
public:
float setPointDegF;
vector<Wall> walls; //TODO consider making walls protected / private
private:
string roomName;
vector<int> linkedRooms;
float storedTemperature;
float storedHumidity;
//member functions
void linkToRoom(int roomToLink);
void unlinkFromRoom(int roomToUnlink);
void removeAllLinksToRoom(int roomToUnlink);
void renumberLinkedRoomsAfterErase(int erasedRoom);
public:
//friends
friend class House;
//member functions
void addWalls(unsigned int numWallsToAdd=1);
void removeWall(unsigned int wallToRemove);
//Sensor Functions
void readSensorTemperature();
void readSensorHumidity();
void temperature();
void humidity();
//constuctors / destructors
Room();
virtual ~Room();
//STL linked list overload operators =, ==, and <
Room &operator=(const Room &rhs);
int operator==(const Room &rhs) const;
int operator<(const Room &rhs) const;
private:
void getAttachedRooms(Wall& tempWall);
bool wallDoesNotExist(unsigned int wallToRemove);
bool roomLinked(int roomToLink);
bool roomNotLinked(int roomToLink);
vector<int>::iterator findRoom(int roomToFind);
vector<int>::iterator findInsertionPoint(int roomToInsert);
};
rooms.erase(roomIterator)
will delete the element at location roomIterator
. rooms.erase(roomIterator)
将删除位置roomIterator
的元素。 If you want to delete all elements from beginning to roomToRemove
, use rooms.erase(rooms.begin(), rooms.begin()+roomToRemove)
or rooms.erase(rooms.begin(), roomIterator)
如果要删除从开始到
roomToRemove
所有元素,请使用rooms.erase(rooms.begin(), rooms.begin()+roomToRemove)
或rooms.erase(rooms.begin(), roomIterator)
nm answered this in the comments below my question, but I am retyping it here for clarity: nm在我的问题下方的评论中回答了此问题,但为了清楚起见,我在此处重新键入:
Also, does the Room class have an assignment operator and a copy constructor defined correctly?
另外,Room类是否正确定义了赋值运算符和副本构造函数? – nm
–纳米
Answer: No. An explicit Room class assignment operator was causing the issue. 答:否。一个明确的Room类分配运算符导致了此问题。
I tried to overload the default assignment operator and failed miserably.
我试图重载默认赋值运算符,但失败了。 When I commented out that code and used the implicit &= operator, life is good.
当我注释掉该代码并使用隐式&=运算符时,生活是美好的。 Problem solved.
问题解决了。 -neghzero
-neghzero
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.