c++11 foreach 语法和自定义迭代器
我正在为一个容器编写一个迭代器,用于代替 STL 容器。目前,STL 容器在许多地方都使用 c++11 foreach 语法< /a> 例如:for(auto &x: C)
。我们需要更新代码以使用包装 STL 容器的自定义类:
template< typename Type>
class SomeSortedContainer{
std::vector<typename Type> m_data; //we wish to iterate over this
//container implementation code
};
class SomeSortedContainerIterator{
//iterator code
};
How do I get auto to use the正确的迭代器用于自定义容器,以便能够通过以下方式调用代码?:
SomeSortedContainer C;
for(auto &x : C){
//do something with x...
}
一般来说,什么是需要确保 auto 对类使用正确的迭代器?
I am writing an iterator for a container which is being used in place of a STL container. Currently the STL container is being used in many places with the c++11 foreach syntax eg: for(auto &x: C)
. We have needed to update the code to use a custom class that wraps the STL container:
template< typename Type>
class SomeSortedContainer{
std::vector<typename Type> m_data; //we wish to iterate over this
//container implementation code
};
class SomeSortedContainerIterator{
//iterator code
};
How do I get auto to use the correct iterator for the custom container so the code is able to be called in the following way?:
SomeSortedContainer C;
for(auto &x : C){
//do something with x...
}
In general what is required to ensure that auto uses the correct iterator for a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了能够使用基于范围的 for,您的类应该提供 const_iterator begin() const 和 const_iterator end() const 成员。您还可以重载全局
begin
函数,但我认为拥有成员函数更好。还建议使用iterator begin()
和const_iterator cbegin() const
,但不是必需的。如果您只想迭代单个内部容器,那非常简单:如果您想迭代任何自定义内容,您可能必须将自己的迭代器设计为容器内的类。
有关编写迭代器类的更多详细信息,请参阅我的回答。
To be able to use range-based for, your class should provide
const_iterator begin() const
andconst_iterator end() const
members. You can also overload the globalbegin
function, but having a member function is better in my opinion.iterator begin()
andconst_iterator cbegin() const
are also recommended, but not required. If you simply want to iterate over a single internal container, that's REALLY easy:If you want to iterate over anything custom though, you'll probably have to design your own iterators as classes inside your container.
For more details on writing an iterator class, see my answer here.
您有两种选择:
begin
和end
的成员函数,可以像C.begin()
和C 一样调用它们。结束();
begin
和end
的自由函数,可以使用依赖于参数的查找或在命名空间std
中找到这些函数,并且可以调用例如begin(C)
和end(C)
。You have two choices:
begin
andend
that can be called likeC.begin()
andC.end()
;begin
andend
that can be found using argument-dependent lookup, or in namespacestd
, and can be called likebegin(C)
andend(C)
.正如其他人所说,您的容器必须实现
begin()
和end()
函数(或者具有接受实例的全局或std::
函数)您的容器作为参数)。这些函数必须返回相同的类型(通常是
container::iterator
,但这只是一个约定)。返回的类型必须实现operator*
、operator++
和operator!=
。As others have stated, your container must implement
begin()
andend()
functions (or have global orstd::
functions that take instances of your container as parameters).Those functions must return the same type (usually
container::iterator
, but that is only a convention). The returned type must implementoperator*
,operator++
, andoperator!=
.据我所知,
SomeSortedContainer
只需要提供begin()
和end()
。这些应该返回一个符合标准的前向迭代器,在您的情况下是SomeSortedContainerIterator
,它实际上会包装一个std::vector::iterator
。对于符合标准,我的意思是它必须提供通常的增量和解引用运算符,而且还提供所有这些value_type
、reference_type
、... typedef,它们又由foreach 构造来确定容器元素的基础类型。但您可能只是从std::vector::iterator
转发它们。To my knowledge
SomeSortedContainer
just needs to providebegin()
andend()
. And these should return a standard compliant forward iterator, in your caseSomeSortedContainerIterator
, which would actually wrap astd::vector<Type>::iterator
. With standard compliant I mean it has to provide the usual increment and dereferencing operators, but also all thosevalue_type
,reference_type
, ... typedefs, which in turn are used by the foreach construct to determine the underlying type of the container elements. But you might just forward them from thestd::vector<Type>::iterator
.