或string对象不是常量,那么既能使用iterator也能
使用const_iterator。
begin和end运算符:begin和end返回的具体类型由对象是否是常量决定,如果对象是常量,begin和end返回const_iterator;如果对象不是常量,返回iterator。为了
便于得到const_iterator类型的返回值,引入了两个新函数,分别是cbegin和cend,类似于begin和end,两个新函数也分别返回指示容器第一个元素或最后元素下一个
位置的迭代器。不同的是,不论vector对象(或string对象)本身是否是常量,返回值都是const_iterator。
#include <iostream>
#include <vector>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string s("some string");
if (s.begin() != s.end())
for (auto it = s.cbegin();it != s.cend() && !s.empty();++it)
cout << *it;
cout << endl;
system("pause");
return 0;
}