从std::列表中删除项,并且只能访问迭代器
Remove item from std::list with only having access to the iterator
std::list是一个双链表。这难道不意味着只需访问迭代器就可以从列表中删除项目吗?
也许我的问题不够清楚。
#pragma once
#include <list>
typedef std::list<int> IntList ;
typedef IntList::iterator IntIterator;
class IntHiddenList
{
private:
IntList list;
public:
IntIterator AddInt(int x)
{
list.push_front(x);
return list.begin();
}
};
int main()
{
IntHiddenList a;
IntIterator it = a.AddInt(5);
// How would I go about deleting 5 from the list using only "it"?
}
是的,理论上是可能的。然而,标准库不允许它(它需要容器和迭代器来擦除)。
不管你运气如何:boost提供boost::instrusive
(http://www.boost.org/doc/libs/1_54_0/doc/html/intrusive/list.html)做你想做的事的能力。
否,您仍然需要列表才能删除元素。
在STL中,迭代器只保存指向数据的指针,并提供在容器中移动的操作。你可以在这里看到很好的表格描述。
相关文章:
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何为我的容器实现随机访问迭代器?
- 对于C++随机访问迭代器(矢量迭代器),迭代器之间的差异是如何计算的?
- 对于随机访问迭代器(矢量迭代器),迭代器C++样式指针吗?
- 随机访问迭代器:我错过了什么?
- 有没有办法在C++中制作无锁"counter"随机访问迭代器?
- 如何确保函数模板的参数是随机访问迭代器
- C++基于现有随机访问迭代器的反向迭代器
- 如何实现随机访问迭代器的"less than operator"?
- 随机访问迭代器和Deque
- 在链表上实现随机访问迭代器
- 如何在C++中比较两个非随机访问迭代器
- C++随机访问迭代器超出范围
- 访问迭代器,向量在库存.h
- 具有deque的随机访问迭代器的迭代器无效
- 访问迭代器值会导致segfault 11
- 基于指针的基本随机访问迭代器的代码
- 将多映射迭代器转换为随机访问迭代器
- 在 65536 位置访问迭代器的指针运算符会导致段错误
- 在并行数组上编写随机访问迭代器