设为首页 加入收藏

TOP

C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲(一)
2015-07-20 17:53:43 来源: 作者: 【 】 浏览:3
Tags:array vector lambda 表达式 字符串 操作 中新 类型 多元 new 缓冲
??
  1. 使用C++风格的数组,不需要管理内存。

  2. array要注意不要溢出,因为它是栈上开辟内存.

  3. array适用于任何类型

    #include

    #include

    #include //C++的标准库

    #include //C++字符串

    #include

    using std::array; //静态数组,栈上

    using std::vector; //动态数组,堆上

    using std::string;

    //使用C++风格数组不需要管理内存。

    //array注意不要栈溢出

    //array适用于任何类型

    void main()

    {

    array myint1 = { 1, 2, 3, 4, 5 };

    array myint2 = { 11, 12, 13, 14, 15 };

    array myint3 = { 21, 22, 23, 24, 25 };

    // array , 3> myint = {myint1,myint2,myint3};

    array , 3> myint = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    for (int i = 0; i < myint.size(); i++)//数组大小

    {

    for (int j = 0; j < myint1.size(); j++)

    {

    std::cout << " " << myint[i][j];

    }

    std::cout << "\n";

    }

    std::cin.get();

    }

    1. vector:动态字符串数组

      #include

      #include

      #include //C++的标准库

      #include //C++字符串

      #include

      using std::array;//静态数组,栈上,

      using std::vector;//动态数组,堆上,

      using std::string;

      //使用C++风格数组不需要管理内存。

      //array注意不要栈溢出

      //array适用于任何类型

      void main()

      {

      vector string1;//动态字符串数组

      //可以反复利用

      string1.push_back("notepad");

      string1.push_back("calc");

      string1.push_back("mspaint");

      string1.pop_back();//删除一个

      //string1.clear();//清空

      for (int i = 0; i < string1.size(); i++)//遍历动态数组

      {

      system(string1[i].c_str());

      }

      system("pause");

      }

      5.通过iterator关键字进行过迭代

      #include

      #include

      #include //C++的标准库

      #include //C++字符串

      #include

      using std::array; //静态数组,栈上

      using std::vector; //动态数组,堆上

      using std::string;

      void main()

      {

      vector string1;//动态字符串数组

      string1.push_back("notepad");

      string1.push_back("calc");

      string1.push_back("mspaint");

      vector ::iterator ibegin, iend;//迭代器

      ibegin = string1.begin(); //数据起始点

      iend = string1.end(); //结束

      for (;ibegin != iend;ibegin++)

      {

      string tempstr = *ibegin; //获取指针指向的数据

      system(tempstr.c_str()); //执行指令

      }

      system("pauese");

      }

      6.正逆向迭代数组

      #include

      #include

      #include //C++的标准库

      #include //C++字符串

      #include

      using std::array; //静态数组,栈上

      using std::vector; //动态数组,堆上

      using std::string;

      void main()

      {

      array myint = { 1, 2, 3, 4, 5 };

      array ::iterator ibegin, iend;//正向迭代器

      ibegin = myint.begin();

      iend = myint.end();

      while (ibegin != iend)

      {

      std::cout << *ibegin << std::endl;

      ibegin++;

      }

      std::cout << "----------" << std::endl;

      array ::reverse_iterator rbegin, rend;

      rbegin = myint.rbegin();

      rend = myint.rend();

      while (rbegin != rend)

      {

      std::cout << *rbegin << std::endl;

      rbegin++;

      }

      std::cin.get();

      }

      \

      7.反向迭代器

      #include

      #include

      #include //C++的标准库

      #include //C++字符串

      #include

      using std::array; //静态数组,栈上

      using std::vector; //动态数组,堆上

      using std::string;

      void main()

      {

      vector string1; //动态字符串数组

      string1.push_back("notepad");

      string1.push_back("calc");

      string1.push_back("mspaint");

      //反向迭代器

      vector ::reverse_iterator rbegin = string1.rbegin();

      vector ::reverse_iterator rend = string1.rend();

      //rend最后不指向数据,指向数据的结尾的下一个节点

      //当使用下面的方法时,只打印了记事本,计算器

      rend--;

      A:if (rbegin != rend)

      {

      system((*rend).c_str()); //执行指令

      //rbegin++;

      rend--;

      goto A;

      }

      }

      8.lambda表达式,不仅仅适用与array,也适用于vector

      #include

      #include

      #include //算法 lambda表达式,不仅仅适用于array,也适用于vector

      void main()

      {

      std::vector myvector;

      myvector.push_back(11);

      myvector.push_back(22);

      myvector.push_back(33);

      myvector.push_back(3);

      myvector.push_back(4);

      myvector.push_back(5);

      int res = 0; //结果

      //&res直接操作一个变量,res等价于返回值,x代表参数,

      //每次充当迭代器指向的元素,大括号就是代码

      std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });

      std::cout << res;

      std::cin.get();

      }

      运行结果是:

      \

      9.vector的增删改查

      #include

      #include

      #include //算法 lambda表达式,不仅仅适用于array,也适用于vector

      void main()

      {

      //分配5个空间,默认初始化为0

      std::vector myvector(5);

      myvector.push_back(1);

      myvector.push_back(11);

      myvector.push_back(111);

      myvector.push_back(1

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇引用内部函数绑定机制,R转义字符.. 下一篇bnu 34988 Happy Reversal

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: