How to remove constness of const_iterator?

后端 未结 9 2508
囚心锁ツ
囚心锁ツ 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条回答
  •  庸人自扰
    2020-11-27 04:21

    you can convert your const iterator value pointer to a non const value pointer and use it directly something like this

        vector v;                                                                                                         
    v.push_back(0);
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    vector::const_iterator ci = v.begin() + 2;
    cout << *ci << endl;
    *const_cast(&(*ci)) = 7;
    cout << *ci << endl;
    

提交回复
热议问题