c++ - 恒定时间删除 STL 容器中的元素

标签 c++ stl

简单的问题:是否有任何STL容器允许我在恒定时间内删除当前迭代器位置?
提前致谢!

最佳答案

std::listerase具有复杂性:

Complexity

1) Constant.

2) Linear in the distance between first and last.

对于情况 2,您对一系列元素使用删除。

来自文档,std::list<T>::erase看起来像这样:

//(1)
iterator erase( iterator pos );
iterator erase( const_iterator pos );
//(2)   
iterator erase( iterator first, iterator last );
iterator erase( const_iterator first, const_iterator last );

另一个是std::forward_list (不完全一样,因为它只有 erase_after )

关于c++ - 恒定时间删除 STL 容器中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372786/

相关文章:

c++ - 找出数组中有多少个不同的浮点值

c++ - wxWidgets画波形比qt更好是因为原生函数支持吗?

模板函数 <queue> 中的 C++ 内存错误

c++ - 如何编写可以采用任意容器(类型为 'operator<<' )的流 'X'?

c++ - 如何计算给定 vector 索引的补码?

c++ - 我的 fahrenheit-celcius 程序忽略了我的 if-else 语句,并在我每次运行该程序时将值更改为 0

c++ - 如何使 friend 类的层次结构?

c++ - 所有权和 move 语义

c++ - 对 std::sort 使用比较器函数时出现异常错误

c++ - CGAL:带有信息的点的凸包