首页 > 编程语言 > 详细

stout代码分析之九:c++11容器新特性

时间:2016-09-20 08:56:42      阅读:224      评论:0      收藏:0      [点我收藏+]

  stout大量使用了c++11的一些新特性,使用这些特性有利于简化我们的代码,增加代码可读性。以下将对一些容器的新特性做一个总结。主要两方面:

  • 容器的初始化,c++11中再也不用手动insert或者push_back来初始化了
  • 容器的遍历,c++11中再也不用使用冗长的迭代器遍历了

  让我们一睹为快吧:

#include <map>
#include <string>
#include <iostream>
#include <vector>

int main()
{
  std::vector<int> a = {1, 2, 3};
  std::map<int, std::string> b = {{1, "one"}, {2, "two"}, {3, "three"}};
  
  for(auto& elem : a)
    std::cout << elem << std::endl;

  for (auto& kv : b)
    std::cout << kv.first << " : " << kv.second << std::endl;

  return 0;
}

stout代码分析之九:c++11容器新特性

原文:http://www.cnblogs.com/taiyang-li/p/5887386.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!