How to remove constness of const_iterator?

后端 未结 9 2490
囚心锁ツ
囚心锁ツ 2020-11-27 03:21

As an extension to this question Are const_iterators faster?, I have another question on const_iterators. How to remove constness of a const_iterator

9条回答
  •  -上瘾入骨i
    2020-11-27 04:23

    I believe this conversion is not needed in good designed program.

    If you need do this - try redesign code.

    As workaround you can do next:

    typedef std::vector< size_t > container_type;
    container_type v;
    // filling container code 
    container_type::const_iterator ci = v.begin() + 3; // set some value 
    container_type::iterator i = v.begin();
    std::advance( i, std::distance< container_type::const_iterator >( v.begin(), ci ) );
    

    But I'm think that sometimes this conversion is impossible, because your algorithms can haven't access to container.

提交回复
热议问题