【发布时间】:2016-05-13 09:19:26
【问题描述】:
我有一个存储另一个容器的容器。我需要在嵌套容器的值上为这个容器创建一个迭代器。这个迭代器应该定义一个operator*,但我不知道要返回的嵌套容器的值类型。我不能保证容器有任何typedef 的值。唯一给出的是每个容器都有一个正确的operator[]。如何声明operator*?
template<typename ContainerType>
struct MyIterator{
MyIterator(ContainerType& container, int32 indexA = 0, int32 indexB = 0)
: Container(container)
, IndexA(indexA)
, IndexB(indexB)
{}
// NestedValueType needs to be replaced or declared somehow
NestedValueType& operator* ()
{
return Container[IndexA][IndexB];
}
private:
ContainerType& Container;
int32 IndexA;
int32 IndexB;
};
附:我只有 C++11 功能可用。
【问题讨论】:
标签: c++ templates c++11 containers