MapTest.cpp

#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include "MapTest.h" using namespace std; void MapTest::simpleEnumeration()
{
map<string,double> coll {
{ "tim", 9.9 },
{ "struppi", 11.77 }
} ; // for range-based enumeration
cout << "for range-based enumeration: " << endl;
for (auto elem : coll)
{
cout << elem.first << ": " << elem.second << endl;
} // iterating
cout << "iterating: " << endl;
map<string, double>::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos)
{
cout << pos->first << ": " << pos->second << endl;
} // square the value of each element:
for_each (coll.begin(), coll.end(),
[] (pair<const string,double>& elem) {
elem.second *= elem.second;
}); // print each element:
cout << "for_each lambda enumeration: " << endl;
for_each (coll.begin(), coll.end(),
[] (const map<string,double>::value_type& elem) {
cout << elem.first << ": " << elem.second << endl;
});
} void MapTest::run()
{
printStart("simpleEnumeration()");
simpleEnumeration();
printEnd("simpleEnumeration()");
}

运行结果:

--------------- simpleEnumeration(): Run Start ----------------
for range-based enumeration:
truppi: 11.77
im: 9.9
terating:
truppi: 11.77
im: 9.9
or_each lambda enumeration:
truppi: 138.533
im: 98.01
--------------- simpleEnumeration(): Run End ----------------

STL - 容器 - Map(一)的更多相关文章

  1. STL容器 -- Map

    核心描述: map 就是从键(key) 到 值(value) 的一个映射.且键值不可重复,内部按照键值排序. 头文件: #include <map> 拓展: multimap 是一个多重映 ...

  2. STL容器Map

    Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)

  3. STL - 容器 - Map(二)

    把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> ...

  4. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  5. STL容器——对map排序

    STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...

  6. 【STL容器学习】-关联容器与map的用法

    STL提供了4个关联容器:set.multiset.map和multimap.这些容器提供了通过keyword高速存储和訪问数据元素的能力.Set和map不同意有反复keyword,而multiset ...

  7. C++ STL 中 map 容器

    C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...

  8. 使用C++STL的map容器实现一种命令映射

    因为最近在练习写一个ftp的服务器,其中的命令有很多种,每个命令对应一个执行函数,能够想到的最简单的实现方式便是使用if--else匹配命令和执行对应的函数,如下所示: if(strcmp(" ...

  9. C++STL之map映照容器

    map映照容器 map映照容器的元素数据是由一个键值和一个映照数据组成的, 键值与映照数据之间具有一一映照关系. map映照容器的数据结构也是采用红黑树来实现的, 插入元素的键值不允许重复, 比较函数 ...

随机推荐

  1. bzoj 1312 最大密度子图

    晕,m=0是要输出1(弄的我还找管理员要数据,但明显题意是叫我们输出0呀) 最大密度子图,把边转换成点,然后二分答案,跑最大权闭合子图判定是否可行. #include <cstdio> # ...

  2. pygame系列_第一个程序_图片代替鼠标移动

    想想现在学校pygame有几个钟了,就写了一个小程序:图片代替鼠标移动 程序的运行效果: 当鼠标移动到窗口内,鼠标不见了,取而代之的是图片..... ========================= ...

  3. git一些命令

    ************git基本命令***************git config --global user.name "xielehe" 设置名字git config - ...

  4. Swift3.0字符串相关操作

    以下有关字符串的常用操作都可直接复制到Xcode中进行验证,如发现错误,请在评论区留言指正! 1.字符串的定义 var str1="hello, swift." //字符串变量 相 ...

  5. MAIN/autoslb.py · 林語/autoslb - 码云 - 开源中国

    MAIN/autoslb.py · 林語/autoslb - 码云 - 开源中国 CloudXNS

  6. 如何在ubuntu安装phpstorm

    第一步:使用组合键ctrl+alt+t 打开Terminal,cd /home/xxx(当前登录用户名)/downloads(下载目录) 第二步:下载 phpstorm 附截止发文最新版本链接:htt ...

  7. Delphi 调用SQL Server 2008存储过程

    1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...

  8. mysql文件目录详解 LINUX

    http://www.cnblogs.com/yjf512/archive/2012/12/11/2813398.html

  9. Latex Error cannot determine the size of graphic 报错的解决的方法

    Latex Error cannot determine the size of graphic 报错的解决的方法 插入jpg文件老是会报错... 追究了半天,原来是编译的命令又问题,不应该使用 la ...

  10. nginx简单代理配置

    原文:https://my.oschina.net/wangnian/blog/791294 前言  Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器, ...