从向量中擦除后的重复指针
Duplicate pointers after after erase from vector
我已经重载了 operator-=() 以从指针向量中删除元素(指针是唯一的,因此不需要删除所有出现的地方,一旦指针被擦除就可以终止循环):
Rooms& Rooms::operator-=(Course *c) {
for (Iter i = rooms.begin(); i != rooms.end(); ++i) {
if (**i == *c) {
*i = NULL;
i = rooms.erase(i);
break;
}
}
return *this;
}
问题是,在应用于向量后,我得到了向量最后一个元素的重复指针。之前:
-------------------------------------------------------------------
| POL | CAE | RUS | ENG | BUS | JPY | | | | | |
| G 1 | G 1 | G 2 | G 2 | G 2 | G 2 | | | | | |
| 9 | 9 | 10 | 10 | 10 | 10 | | | | | |
删除 ENG 和 BUS 后:
-------------------------------------------------------------------
| POL | CAE | RUS | JPY | JPY | JPY | | | | | |
| G 1 | G 1 | G 2 | G 2 | G 2 | G 2 | | | | | |
| 9 | 9 | 10 | 10 | 10 | 10 | | | | | |
应该更改什么才能实际得到结果:
-------------------------------------------------------------------
| POL | CAE | RUS | JPY | | | | | | | |
| G 1 | G 1 | G 2 | G 2 | | | | | | | |
| 9 | 9 | 10 | 10 | | | | | | | |
任何帮助将不胜感激。
编辑:
我的打印函数是这样的:
std::ostream& operator<<(std::ostream& out, const Rooms& rs) {
std::vector<std::string> output(3);
std::ostringstream temp;
for (int i = 0; i < rs.rooms.capacity(); ++i) {
if (rs.rooms[i]) {
temp << "| " << rs.rooms[i]->getCode() << " ";
output[0] += temp.str();
temp.str("");
temp << "| G" << std::setw(CODE_LENGTH - 1)
<< rs.rooms[i]->getGroup() << " ";
output[1] += temp.str();
temp.str("");
temp << "| " << std::setw(CODE_LENGTH)
<< rs.rooms[i]->getSize() << " ";
output[2] += temp.str();
temp.str("");
} else {
output[0] += "| ";
output[1] += "| ";
output[2] += "| ";
}
}
output[0] += "|";
output[1] += "|";
output[2] += "|";
out << printHorizont(rs.size) << output[0] << std::endl
<< output[1] << std::endl << output[2] << std::endl;
return out;
}
看起来很复杂,但找不到更好的打印方式。
在operator<<()
,
for (int i = 0; i < rs.rooms.capacity(); ++i) {
对于rs.rooms.size() <= i < rs.rooms.capacity()
,基本上rs.rooms[i]
是不合法的
使用迭代器遍历向量,你永远不会犯这样的错误。如果你真的需要打印那些"empty spaces",你可以写成
for (int i = 0; i < rs.rooms.capacity(); ++i) {
if (i < rs.rooms.size() && rs.rooms[i]) {
// access rs.rooms[i]
} else {
// print empty placeholder
}
}
我已经重载了 operator-=() 以从指针向量中删除元素(指针是唯一的,因此不需要删除所有出现的地方,一旦指针被擦除就可以终止循环):
Rooms& Rooms::operator-=(Course *c) {
for (Iter i = rooms.begin(); i != rooms.end(); ++i) {
if (**i == *c) {
*i = NULL;
i = rooms.erase(i);
break;
}
}
return *this;
}
问题是,在应用于向量后,我得到了向量最后一个元素的重复指针。之前:
-------------------------------------------------------------------
| POL | CAE | RUS | ENG | BUS | JPY | | | | | |
| G 1 | G 1 | G 2 | G 2 | G 2 | G 2 | | | | | |
| 9 | 9 | 10 | 10 | 10 | 10 | | | | | |
删除 ENG 和 BUS 后:
-------------------------------------------------------------------
| POL | CAE | RUS | JPY | JPY | JPY | | | | | |
| G 1 | G 1 | G 2 | G 2 | G 2 | G 2 | | | | | |
| 9 | 9 | 10 | 10 | 10 | 10 | | | | | |
应该更改什么才能实际得到结果:
-------------------------------------------------------------------
| POL | CAE | RUS | JPY | | | | | | | |
| G 1 | G 1 | G 2 | G 2 | | | | | | | |
| 9 | 9 | 10 | 10 | | | | | | | |
任何帮助将不胜感激。
编辑:
我的打印函数是这样的:
std::ostream& operator<<(std::ostream& out, const Rooms& rs) {
std::vector<std::string> output(3);
std::ostringstream temp;
for (int i = 0; i < rs.rooms.capacity(); ++i) {
if (rs.rooms[i]) {
temp << "| " << rs.rooms[i]->getCode() << " ";
output[0] += temp.str();
temp.str("");
temp << "| G" << std::setw(CODE_LENGTH - 1)
<< rs.rooms[i]->getGroup() << " ";
output[1] += temp.str();
temp.str("");
temp << "| " << std::setw(CODE_LENGTH)
<< rs.rooms[i]->getSize() << " ";
output[2] += temp.str();
temp.str("");
} else {
output[0] += "| ";
output[1] += "| ";
output[2] += "| ";
}
}
output[0] += "|";
output[1] += "|";
output[2] += "|";
out << printHorizont(rs.size) << output[0] << std::endl
<< output[1] << std::endl << output[2] << std::endl;
return out;
}
看起来很复杂,但找不到更好的打印方式。
在operator<<()
,
for (int i = 0; i < rs.rooms.capacity(); ++i) {
对于rs.rooms.size() <= i < rs.rooms.capacity()
,基本上rs.rooms[i]
是不合法的
使用迭代器遍历向量,你永远不会犯这样的错误。如果你真的需要打印那些"empty spaces",你可以写成
for (int i = 0; i < rs.rooms.capacity(); ++i) {
if (i < rs.rooms.size() && rs.rooms[i]) {
// access rs.rooms[i]
} else {
// print empty placeholder
}
}