轉自:https://cplusplus.com/reference/vector/vector/cbegin/
1.例子
返回一個指向容器首個元素的常量型別迭代器const_iterator:
int main() { std::vector<int> myvector = {10,20,30,40,50}; std::cout << "myvector contains:"; for (auto it = myvector.rbegin(); it != myvector.rend(); ++it){ //*it =5;//如果這裡嘗試要修改,那麼編譯時會報錯error: assignment of read-only location ‘it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<const int*, std::vector<int> >()’ std::cout << ' ' << *it; } cout<<endl; return 0; }
而使用普通的begin()/end()可以直接透過*it修改值。