我正在为一个被用作STL容器替代品的容器编写迭代器。目前,许多地方都使用了STL容器并使用c++11 foreach语法,例如: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
};
如何让auto使用正确的迭代器来遍历自定义容器,以便代码能够按以下方式调用:
SomeSortedContainer C;
for(auto &x : C){
//do something with x...
}
一般而言,如何确保auto使用一个类的正确迭代器?