STL - 容器 - Map(一)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: MapTest.cpp #include #include #include #include #include "MapTest.h" using namespace std; void MapTest::simpleEnumeration() { map coll { { "tim", 9.

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

 

目录
相关文章
|
4月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
100 2
|
4月前
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
90 5
|
4月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
103 2
|
6月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
7月前
|
存储 C++ 索引
|
7月前
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
94 0
|
7月前
|
存储 C++ 索引
C++基础知识(八:STL标准库 Map和multimap )
C++ 标准模板库(STL)中的 map 容器是一种非常有用的关联容器,用于存储键值对(key-value pairs)。在 map 中,每个元素都由一个键和一个值组成,其中键是唯一的,而值则可以重复。
109 0
|
7月前
|
存储 算法 C语言
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
【C++】详解STL的适配器容器之一:优先级队列 priority_queue
|
4天前
|
Ubuntu API 网络虚拟化
ubuntu22 编译安装docker,和docker容器方式安装 deepseek
本脚本适用于Ubuntu 22.04,主要功能包括编译安装Docker和安装DeepSeek模型。首先通过Apt源配置安装Docker,确保网络稳定(建议使用VPN)。接着下载并配置Docker二进制文件,创建Docker用户组并设置守护进程。随后拉取Debian 12镜像,安装系统必备工具,配置Ollama模型管理器,并最终部署和运行DeepSeek模型,提供API接口进行交互测试。
91 15
|
2月前
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
299 78