STL - 容器 - Map(一)

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 ----------------

 

时间: 2024-08-04 05:38:42

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

stl map 下标-STL容器map 下标访问的问题

问题描述 STL容器map 下标访问的问题 STL容器map 下标访问的问题定义了如下的一个map 容器 Key 是int values 是一个结构体typedef struct _prostru{ int jmqnum; int bncnun; _prostru() { jmqnum=-1; bncnun=-1; }}PROSTRU; map m_pro; m_pro[1].jmqnum=5;m_pro[2].bncnum=2; 在进程中 可以用下标访问和修改 结构体中的值线程传入后 是个指针

STL - 容器 - Map(二)

把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> #include <iomanip> #include "MapAdvanceTest.h" #include "../../Core/ContainerUtil.h" using namespace std; void MapAdvanceTest::

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

这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自alvin_lee ,codeproject,codeguru.baidu等等! 先看看alvin_lee 朋友做的解析,我觉得还是很正确的,从算法角度阐述了他们之间的问题! 实际上这个问题不光C++会遇到,其他所有语言的标准容器的实现及选择上都是要考虑的.做应用程序你可能觉得影响不大,但是写算法或者核

浅析stl序列容器(map和set)的仿函数排序_C 语言

问题:set是一个自动有序的集合容器,这是set的一个最实惠的性质,从小到大,只要你插入进去,就有序了.但是,如果你不想要这个顺序呢,是不是可以人为控制set容器的元素顺序呢?答案是,可以的,因为stl也是程序员设计的. 首先看stl的模板构造函数 复制代码 代码如下: explicit set ( const Compare& comp = Compare(), const Allocator& = Allocator() );templateset ( InputIterator fi

用STL的map和算法的find_if实现名表表示和查找功能

问题描述 用STL的map和算法的find_if实现名表表示和查找功能 #include #include #include #include using namespace std; bool older_than_20(int birthdate) { return (2016 - birthdate / 10000)>20; } int main() { maptable_item;//创建一个map类容器,用于存储名表 //创建名表 table_item["Charles"

c++-以下代码对stl容器DataMap进行了多少次搜索

问题描述 以下代码对stl容器DataMap进行了多少次搜索 这是一道笔试题,想了好久.找了好久也没有个确切的答案,求大神指导 struct Data{ Data():value(0){} int value; } std::map<int, Data> DataMap; void RemoveData(int key){ assert(DataMap.empty() == false); if(DataMap[key].value > 0) --DataMap[key].value;

从零开始_学_数据结构(五)——STL(map、set、list、vector)

STL容器   前注: STL(标准模板库)是一个C++的软件库,也是C++标准程序库的一部分. 这些容器,应该都是STL里面的一个类. vector封装数组.list封装链表.map和set封装二叉树   一.list 在不懂的时候,list可以理解为双向链表(很像,但事实上不是). (1)声明一个list对象: ①包含头文件list:#include<list> ②声明他:std::list<int> one; //声明一个list对象 ③需要注意,list位于std名称空间之

【C/C++学院】0828-STL入门与简介/STL容器概念/容器迭代器仿函数算法STL概念例子/栈队列双端队列优先队列/数据结构堆的概念/红黑树容器

STL入门与简介 #include<iostream> #include <vector>//容器 #include<array>//数组 #include <algorithm>//算法 using namespace std; //实现一个类模板,专门实现打印的功能 template<class T> //类模板实现了方法 class myvectorprint { public: void operator ()(const T &

ACM STL容器和算法

1.4      STL 的组成 STL有三大核心部分:容器(Container).算法(Algorithms).迭代器(Iterator),容器适配器(container adaptor),函数对象(functor),除此之外还有STL其他标准组件.通俗的讲: 容器:装东西的东西,装水的杯子,装咸水的大海,装人的教室--STL里的容器是可容纳一些数据的模板类. 算法:就是往杯子里倒水,往大海里排污,从教室里撵人--STL里的算法,就是处理容器里面数据的方法.操作. 迭代器:往杯子里倒水的水壶,