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

  • 容器的初始化,c++11中再也不用手动insert或者push_back来初始化了
  • 容器的遍历,c++11中再也不用使用冗长的迭代器遍历了
  • 容器的emplace,避免了一次赋值构造操作和一次析构操作
  • 增加了unordered_map容器
  • 增加了哈希函数std::hash

  让我们一睹为快吧:

  std::hash

#include <functional>
#include <string>
#include <iostream>
#include <map> int main()
{
std::hash<std::string> str_hash;
std::string a = "foo";
std::string b = "foo";
std::string c = "bar"; std::cout << (str_hash(a) == str_hash(b) ? "hash(a) == hash(b)" : "hash(a) != hash(b)") << std::endl;
std::cout << (str_hash(a) == str_hash(c) ? "hash(a) == hash(c)" : "hash(a) != hash(c)") << std::endl; std::map<int, std::string> d;
d.emplace(, "one");
for(auto mit : d)
{
std::cout << mit.first << ":" << mit.second << std::endl;
}
return ;
}

    容器emplace:

#include <vector>
#include <string>
#include <iostream> class A
{
public:
A(const std::string& name) : name_(name)
{
std::cout << "constructor1" << std::endl;
} A(const A& other) : name_(other.name_)
{
std::cout << "constructor2" << std::endl;
} ~A()
{
std::cout << "destructor" << std::endl;
} private:
std::string name_;
}; int main()
{
std::vector<A> a;
a.push_back(A("foo"));
std::cout << "push back finish" << std::endl; a.emplace(a.begin(), "bar");
std::cout << "emplace finish" << std::endl; std::cout << "to exit" << std::endl;
return ;
}

  容器初始化和遍历

#include <map>
#include <string>
#include <iostream>
#include <vector> int main()
{
std::vector<int> a = {, , };
std::map<int, std::string> b = {{, "one"}, {, "two"}, {, "three"}}; for(auto& elem : a)
std::cout << elem << std::endl; for (auto& kv : b)
std::cout << kv.first << " : " << kv.second << std::endl; return ;
}

stout代码分析之九:c++11容器新特性的更多相关文章

  1. WebShell代码分析溯源(九)

    WebShell代码分析溯源(九) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];$arr = array($_POST['pass'] => '|.*|e', ...

  2. 【C++11】新特性——Lambda函数

    本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...

  3. atitit.Oracle 9 10 11 12新特性attilax总结

    atitit.Oracle 9  10 11  12新特性 1. ORACLE 11G新特性 1 1.1. oracle11G新特性 1 1.2. 审计 1 1.3. 1.   审计简介 1 1.4. ...

  4. C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)

    1. 概要   本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...

  5. Qt5 中对 C++11 一些新特性的封装

    在 Qt5 中,提供更多 C++11 的特性支持,接下来我们将进行详细的说明. slots (槽) 的 Lambda 表达式 Lambda表达式 是 C++11 中的一个新语法,允许定义匿名函数.匿名 ...

  6. 【Qt开发】Qt5 中对 C++11 一些新特性的封装

    C++11 是现在的 C++ 标准的名称,C++11 为 C++ 语言带来很多新特性. 而 Qt 4.8 是 Qt 首个在其 API 中开始使用一些新的 C++11 特性的版本,我之前写过一篇博文:C ...

  7. c++11的新特性

    好奇心来源于下面的一段代码, 一个是unordered_map, 这是c++11新加的container. 另外还有unordered_set, unordered_multimap, unorder ...

  8. C# 11 的新特性和改进前瞻

    前言 .NET 7 的开发还剩下一个多月就要进入 RC,C# 11 的新特性和改进也即将敲定.在这个时间点上,不少新特性都已经实现完毕并合并入主分支 C# 11 包含的新特性和改进非常多,类型系统相比 ...

  9. 【C++11】新特性——auto的使用

    [C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...

随机推荐

  1. VMWare Workstation新装CentOS 7无法联网解决办法

    按照这位博主的方法成功解决:http://blog.csdn.net/deniro_li/article/details/54632949

  2. 【转】Unity 使用xLua遇到的坑

    在我们使用xLua作为Unity中lua集成的解决方案时,遇到了一个问题,就是当我们使用在lua中把UI中的某个控件绑定相应的事件(如按钮的onClick事件),xLua绑定这个事件是用委托实现的,具 ...

  3. Period :KMP

    I - Period Problem Description For each prefix of a given string S with N characters (each character ...

  4. 简析@Resource 和 @Autowired的区别

    @Autowird @Autowird 属于spring框架,默认使用类型(byType)进行注入,例如下面代码: @Autowired public IUserService userService ...

  5. DataSet转化为DataTable

    . DataTable dt = ds.Tables[]; . DataTable dt = dao.FillTables("GetOptions_DKI_City_HCPName" ...

  6. Centos/Linux 下升级python2.7至3.5.0

    (一) 安装Python3.5 (1)在安装python之前,因为linux系统下默认没有安装wget,gcc,首先安装wget,gcc: [root@node6 python_scripts]# y ...

  7. Icingaweb2监控oracle数据库的安装配置流程

    Icinga2安装配置check_oracle_health流程 1.安装 由于check_oracle_health是使用perl语言编写的,因此在安装该插件之前,首先要安装oracle的客户端实例 ...

  8. Notes of the scrum meeting before publishing(12.17)

    meeting time:18:30~20:30p.m.,December 17th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  9. LintCode-532.逆序对

    逆序对 在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.给你一个数组,求出这个数组中逆序对的总数. 概括:如果a[i] > a[j] 且 i < j, a[i ...

  10. Swift-创建UIButton(其他UI组件雷同)

    let button = UIButton.init(frame: CGRectMake(, , , )) button.setTitle("按钮", forState: UICo ...