首先,我相对来说是个新手。我有一个类型的向量:boost::container::vector<std::string> plates和我用
for ( unsigned int k = 0; k < plates.size(); k++ ),到目前为止还不错。
现在,我需要从循环中擦除元素,我尝试了以下步骤:plates.erase(plates.begin()+k);,但是这给了我下面的输出并终止了我的应用程序:
include/boost/container/vector.hpp:1595: boost::container::vector<T, Allocator, Options>::reference boost::container::vector<T,
, Options>::size_type) [with T = std::__cxx11::basic_string<char>; Allocator = boost::container::new_allocator<std::__cxx11::basic_string<char> >; Options = void; boost
:basic_string<char>&; boost::container::vector<T, Allocator, Options>::size_type = long unsigned int]: Assertion `this->m_holder.m_size > n' failed.我在这里做错什么了?我的循环如下所示,其中foo()返回一个指针或NULL:
for ( unsigned int k = 0; k < plates.size(); k++ ) {
if (foo(&lpmap, plates[k]) != NULL){
std::cout << "DEBUG: erase" << std::endl;
plates.erase(plates.begin()+k);
} else {
std::cout << "print " << plates[k] << std::endl;
}
}编辑1
for ( unsigned int k = 0; k < plates.size();) {
if (foo(&lpmap, plates[k]) != NULL){
std::cout << "DEBUG: erase" << std::endl;
plates.erase(plates.begin()+k);
} else {
std::cout << "print " << plates[k] << std::endl;
k++;
}
}发布于 2018-06-28 15:03:01
boost中有一个断言,用于检查是否试图访问超出范围的索引。因此,如果您使用的plates[k]与k比实际大小更多,您将得到一个断言。
您可以在boost代码:https://www.boost.org/doc/libs/master/boost/container/vector.hpp中看到检查。
https://stackoverflow.com/questions/51085889
复制相似问题