相关疑难解决方法(0)

const到非const迭代器比较,它们是否有效

我有两个迭代器到一个容器,一个const和一个非const.比较它们是否存在问题,看它们是否都引用了容器中的同一个对象?这是一个通用的C++ 11迭代器问题:

可以合法地比较const和非const迭代器,看看它们是否都引用同一个对象,与容器的类型无关(即,它们都是保证引用同一容器中的对象或容器末端的迭代器) (),但一个是const而另一个不是)?

例如,请考虑以下代码:

some_c++11_container container;

// Populate container
...

some_c++11_container::iterator iObject1=container.begin();
some_c++11_container::const_iterator ciObject2=container.cbegin();

// Some operations that move iObject1 and ciObject2 around the container
...

if (ciObject2==iObject1) // Is this comparison allowed by the C++11 standard?
  ...; //Perform some action contingent on the equality of the two iterators
Run Code Online (Sandbox Code Playgroud)

c++ c++11

15
推荐指数
1
解决办法
1959
查看次数

标签 统计

c++ ×1

c++11 ×1