鱼C论坛

 找回密码
 立即注册
查看: 2358|回复: 0

[技术交流] C++上天之路第四十七课到四十八课(容器和算法)

[复制链接]
发表于 2017-7-19 16:45:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
第四十七课到四十八课(容器和算法)
        容器:像栈那样定义好的数据结构,前辈们集合放在了C++标准库中
        如:vector(向量)就是一个可以无限延长的数组:std::vector<type> names
        用size可以查看当前的长度,push_back(xxx)可以添加东西

        迭代器:对容器里的内容进行一个遍历。
        C++标准库提供的迭代器iterator,相当于一个特殊的指针,对所有的容器都可以用
        std::vector<std::string>::iterator  iter = names.begin();
       

        c++算法头文件<algorithm>


47:
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>


  4. void main()
  5. {
  6.         std::vector<std::string> names;

  7.         names.push_back("第一个");
  8.         names.push_back("第二个");
  9.         names.push_back("第三个");

  10.         for(int i=0; i< names.size(); i++)
  11.         {
  12.                 std::cout << names[i] << '\n';
  13.         }
  14. }
复制代码


48:
1.
  1. #include<iostream>
  2. #include<string>
  3. #include<vector>


  4. void main()
  5. {
  6.         std::vector<std::string> names;

  7.         names.push_back("第一个");
  8.         names.push_back("第二个");
  9.         names.push_back("第三个");

  10.         std::vector<std::string>::iterator iter = names.begin();

  11.         while( iter != names.end() )
  12.         {
  13.                 std::cout << *iter << "\n";
  14.                 iter++;
  15.         }
  16. }
复制代码

  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<algorithm>

  5. void  main()
  6. {
  7.         std::vector<std::string> names;
  8.        
  9.         names.push_back("Larry");
  10.         names.push_back("Barry");
  11.         names.push_back("Lilei");
  12.         names.push_back("Candy");
  13.         names.push_back("Xiaoqin");
  14.         names.push_back("Mark");
  15.         names.push_back("Siple");

  16.         std::sort(names.begin(),names.end());

  17.         std::vector<std::string>::iterator iter = names.begin();

  18.         while( iter != names.end() )
  19.         {
  20.                 std::cout << *iter << '\n';
  21.                 iter++;
  22.         }

  23. }
复制代码

评分

参与人数 1鱼币 +1 收起 理由
小甲鱼 + 1

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表