zoukankan      html  css  js  c++  java
  • C++11常用特性介绍——for循环新用法

    一、for循环新用法——基于范围的for循环

      for(元素类型 元素对象 : 容器对象)

      {

        //遍历

      }

      1)遍历字符串

      std::string str = "hello world";

      for(auto ch : str)

      {

        std::cout << ch << std::endl;

      }

      2)遍历数组

      int arr[] = {1,2,3,4,5};

      for(auto i : arr)

      {

        std::cout << i << std::endl;

      }

      //不用知道数组容器的大小,即可方便的遍历数组。

      2)遍历stl容器

      vector<int> v = {1,2,3,4,5};

      for(atuo& i : v)

      {

        std::cout << i << std::endl;

      }

      //通过引用可以修改容器内容

      3)遍历stl map

      std::map<int,int> map = { {1,1},{2,2},{3,3} };

      for(auto i : map)

      {

        std::cout << i.first << i.second << std::endl;

      }

      遍历map返回的是pair变量,不是迭代器。

  • 相关阅读:
    js(四) 全选/全不选和反选
    js(三) ajax异步局部刷新技术底层代码实现
    js(二) 实现省市联动(json)
    接口
    内部类
    封装
    Static关键字
    this关键字
    带参数的方法
    abstract关键字
  • 原文地址:https://www.cnblogs.com/zhangnianyong/p/11855572.html
Copyright © 2011-2022 走看看