简体   繁体   English

如何从std :: stack获取非常量迭代器 <T, std::vector<T> &gt;?

[英]How do I get non-const iterators from a std::stack<T, std::vector<T>>?

I need to iterate trough the contents of the internal container, however the only method that allows me to access it is constant: 我需要遍历内部容器的内容,但是唯一允许我访问它的方法是恒定的:

const _Container& _Get_container() const
    {   // get reference to container
    return (c);
    }

So I can only get const iterator from the internal vector's begin().Is there any way around this? 所以我只能从内部向量的begin()获取const迭代器,这有什么办法吗?

You can use a const_iterator to iterate over elements just fine, you just can't modify them. 您可以使用const_iterator来迭代元素,而不能修改它们。

If you do want to modify the objects, there's no way to do it without invoking undefined behavior - modifying an object declared as const is illegal. 如果您确实想修改对象,则无法在不调用未定义行为的情况下进行修改-修改声明为const的对象是非法的。

No, there is no such way. 不,没有这样的方法。 The idea behind std::stack (and similar adaptors) is that you will only use the stack interface. std::stack (和类似的适配器)背后的想法是,您将使用stack接口。 And a generic stack only allows inspection of the top element. 通用堆栈仅允许检查顶部元素。

So technically, you don't want a "stack," you want an "iterable stack." 因此,从技术上讲,您不需要“堆栈”,而需要“可迭代的堆栈”。 And the standard library does not provide a container/adaptor for that, so you'll have to resort to one of 而且标准库没有为此提供容器/适配器,因此您必须采用以下方法之一:

  1. implementing your own 实施自己的

  2. using a plain container and maintaining the stack-like access manually 使用普通容器并手动维护类似堆栈的访问


Side note: I really wish the adaptors actually provided iteration functionality; 旁注:我真的希望适配器实际提供迭代功能; after all, they're only adaptors, so they are guaranteed to be backed by an appropriate container (or they could at least provide iteration as long as the backing container does). 毕竟,它们只是适配器,因此可以确保它们由适当的容器支持(或者,只要支持容器可以,它们至少可以提供迭代)。 But it's not currently part of the standard. 但这不是当前标准的一部分。

暂无
暂无

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

相关问题 如何获得 ::iterator of const std::vector<T> - How to get ::iterator of const std::vector<T> std ::可选 <T> 和以非常量引用T的函数 - std::optional<T> and functions taking a non-const reference to T 为什么我不能在 std::set 中的元素上调用非常量成员函数? - Why can't I call a non-const member function on an element in an std::set? 使用混合const和非const元素模拟std :: vector - Simulate std::vector with mixed const and non-const elements 为什么C ++标准库不包含std :: min,std :: max的非const版本? - Why doesn't the C++ standard library include a non-const version of std::min, std::max? 雄辩的解决方案,需要对std :: tuple(即std :: get)进行非常量索引访问 <E> ) - Eloquent solution to needing non-const indexed access of a std::tuple (i.e. std::get<E>) 类型&#39;std :: vector的非常量引用的无效初始化 <Object*> &” - invalid initialization of non-const reference of type ‘std::vector<Object*>&’ 错误:'std::vector 类型的非常量引用的无效初始化<int> &amp;' 来自 'std::vector 类型的右值<int> *'</int></int> - error: invalid initialization of non-const reference of type ‘std::vector<int>&’ from an rvalue of type ‘std::vector<int>*’ 无效初始化类型为&#39;std :: vector的非const引用 <double> &&#39;来自一个类型的左值 - invalid initialization of non-const reference of type ‘std::vector<double>&’ from an rvalue of type 如何传递非常量 std::vector<double> 通过使用 boost::python 引用 Python? - How to pass non-const std::vector<double> by reference to Python with boost::python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM