为了使指针和数组之类的连续数据列表操作更加简单和安全,c++11引入了用于获取
数组,列表,链表之类的序列数据首,尾地址的标准通常函数begin,end和范围的for循环语句
begin返回指向序列首元素的指针,end返回指向序列最后一个元素后一个位置的指针
| 0 | 1 | 2 | 3 | 4 | 5 | null |
begin返回0这个位置的指针,end返回5后面的指针
范围for语句用于遍历数组和STL容器或其他序列
begin(序列)
end(序列)
for(变量声明:序列)
循环体
下面做一个演示吧
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
string s("hello world!");
int n = 0;
for(int i:a)
cout << i << "\t";
cout << endl;
for(auto& i:a) //通过引用改变a数组的值
i *= i;
for(int *p = begin(a);p != end(a);p++)
cout << *p << "\t";
cout << endl;
for(auto &c:s)
{
n++;
c = toupper(c);//改为大写
}
cout << "s have:" << n << " chars" << endl;
for(auto p = begin(s);p != end(s);p++)
cout << *p;
return 0;
}

