简体   繁体   English

从升压向量中删除条目

[英]erasing entry from boost vector

First off, I'm relatively new to boost. 首先,我是新手。 I have a vector of type: boost::container::vector<std::string> plates and i iterate through it with 我有一个向量类型: boost::container::vector<std::string> plates ,我用它遍历它
for ( unsigned int k = 0; k < plates.size(); k++ ) , so far so good. for ( unsigned int k = 0; k < plates.size(); k++ ) ,到目前为止for ( unsigned int k = 0; k < plates.size(); k++ )顺利。

Now, I need to erase elements from within the loop and I attempted the following: plates.erase(plates.begin()+k); 现在,我需要从循环中擦除元素,然后尝试执行以下操作: plates.erase(plates.begin()+k); but that gives me the below output and terminates my application: 但这给了我下面的输出并终止了我的应用程序:

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.

What am I doing wrong here? 我在这里做错了什么? My loop looks something like this where foo() returns a pointer or NULL: 我的循环看起来像这样,其中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;
        }
    }

EDIT 1 编辑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++;
        }
    }

There is an assertion in boost that checks if you try to access an index out of range. boost中有一个断言,用于检查您是否尝试访问超出范围的索引。 so if you used plates[k] with k more than actual size, you get an assertion. 因此,如果您使用的plates[k]的k大于实际大小,则会得到一个断言。

You can see the check right in the boost code: https://www.boost.org/doc/libs/master/boost/container/vector.hpp . 您可以在增强代码中看到该检查: https : //www.boost.org/doc/libs/master/boost/container/vector.hpp

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

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