zoukankan      html  css  js  c++  java
  • c++11新特性之begin,end和基于范围的for循环

    为了使指针和数组之类的连续数据列表操作更加简单和安全,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 << "	";
    	cout << endl;
    	
    	for(auto& i:a) //通过引用改变a数组的值 
    	i *= i;
    	
    	for(int *p = begin(a);p != end(a);p++)
    	cout << *p << "	";
    	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;
    }
    

      

  • 相关阅读:
    如何设置eclipse默认打开文件方式
    CalendarUtil.java
    排班管理
    jquery ajax 发送邮件例子
    C# 打开文件夹和保存文件夹
    C# QQ邮箱授权码发送邮件
    IIS7 http自动跳转到https
    C# 关键字替换
    C# webBrowser 控件赋值
    C# Post提交数据
  • 原文地址:https://www.cnblogs.com/mch5201314/p/11685569.html
Copyright © 2011-2022 走看看