测试代码:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str="hello world";
string::const_iterator it=str.begin();
const string::iterator itt=str.begin();
*it='A'; //error
*itt='A';
it++;
itt++; //error
system("pause");
}通过分析可知:
const_iterator相当于指向常量的指针,即指针(iterator)可以修改,但指向的值不能修改。
const iterator相当于指针常量,即指针指向不能修改,但指向的值可以修改。
本文详细解析了C++中const_iterator和constiterator的区别,解释了const_iterator相当于指向常量的指针,而constiterator相当于指针常量。通过实例演示了两者在修改字符串元素时的不同行为。
506

被折叠的 条评论
为什么被折叠?



