string::cbegin string::cend

const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
注:返回常量迭代器,不能修改

#include <iostream>
#include <string>

using namespace std;

int main()
{
string s1("hello");
string::const_iterator it;
it = s1.cbegin();
cout << *it++ << endl;
cout << *it << endl;
//*it = 'a';//wrong read_only
it = s1.cend() - 1;
cout << *it << endl;
//*it = 'a';//wrong read_only
return 0;
}

猜你喜欢

转载自www.cnblogs.com/xpylovely/p/12083949.html