c++ map 操作学习

#include <iostream>
#include <map>
#include <string>
#include <vector> using namespace std; typedef struct clientInfo_t {
int fd;
long timeOut;
}clientInfo_t; int main(int argc, char **argv)
{ map<int, clientInfo_t> clientMap = {};
clientInfo_t tmpInfo = {}; /*map init*/
for(int i = 3; i < 20; ++i) {
tmpInfo.fd = i;
tmpInfo.timeOut = -1;
//clientMap.insert(pair<int, clientInfo_t>(i, tmpInfo));
clientMap[i] = tmpInfo;
} /*map trave*/
for (auto m = clientMap.begin(); m != clientMap.end(); ++m) {
cout <<"client fd = "<< m->second.fd << " timeOut = " <<m->second.timeOut<<endl;
} tmpInfo.fd = 15;
tmpInfo.timeOut = -1;
/*map find*/
auto m = clientMap.find(15);
if (m != clientMap.end() ) {
m->second.timeOut = 0;
cout <<"map find fd = " <<m->second.fd<<endl;;
} /*map erase*/
for (auto m = clientMap.begin(); m != clientMap.end(); /*do nothing*/) {
if(m->second.fd % 2 == 0) {
m = clientMap.erase(m);
}
else {
++m;
}
} for (auto m = clientMap.begin(); m != clientMap.end(); ++m) {
cout <<"client fd = "<< m->second.fd << " timeOut = " <<m->second.timeOut<<endl;
} /*map insert*/
tmpInfo.fd = 25;
tmpInfo.timeOut = 25;
/*this will replace key 13 use new value*/
clientMap[13] = tmpInfo; /*new insert*/
clientMap[23] = tmpInfo; /*this will insert failed, becase key 17 exist*/
clientMap.insert(pair<int, clientInfo_t>(17, tmpInfo)); /*only not exist key can insert success*/
clientMap.insert(pair<int, clientInfo_t>(27, tmpInfo));
cout <<"===insert element end======"<<endl; for (auto m = clientMap.begin(); m != clientMap.end(); ++m) {
cout <<"client fd = "<< m->second.fd << " timeOut = " <<m->second.timeOut<<endl;
} return 0;
}

C++ map操作——插入、查找、遍历的更多相关文章

  1. C++(十一)— map的插入、查找、删除

    标准库map类型是一种以键-值(key-value)存储的数据类型. map是STL的一个关联容器.它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值 ...

  2. 如何遍历Map操作总结

    Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "123"); ...

  3. Java集合list,map,set区别及遍历

    1.1 List.Set.Map基本区别 1.List,Set都是继承Collection接口,Map不是. 2.List:LinkedList.ArrayList.Vector Set :HashS ...

  4. 如何优雅的传递 stl 容器作为函数参数来实现元素插入和遍历?

    问题背景 开始正文之前,做一些背景铺垫,方便读者了解我的工程需求.我的项目是一个客户端消息分发中心,在连接上消息后台后,后台会不定时的给我推送一些消息,我再将它们转发给本机的其它桌面产品去做显示.后台 ...

  5. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  6. HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...

  7. c/c++ 标准库 map set 插入

    标准库 map set 插入 一,插入操作 有map如下: map<string, size_t> cnt; 插入方法: 插入操作种类 功能描述 cnt.insert({"abc ...

  8. 小白鼠排队(map容器插入数据的四种方法)

    题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...

  9. JavaScript中的Map、Set及其遍历

    Map Map是一组键值对的结构,具有极快的查找速度. Map的定义. //空map设值key-value var m = new Map(); m.set("XiaoMing", ...

随机推荐

  1. 自己用树莓派做了一个电视盒子,还可以看优酷和cctv

    我刚接触树莓派时间不久,安装过raspberry(树莓派官方系统),ubuntu mate,openelec等系统,openelec是一个电视盒子系统,但是我的用的电视机是一个老式的,老是出现闪屏的问 ...

  2. 洛谷P1160——队列安排(双向链表)

    题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...

  3. EditPlus配置C/C++运行环境

    1.安装MinGW和EditPlus 2.打开EditPlus ctrl+1 编译 ctrl+2 运行

  4. 搭建http文件服务器 - python3使用http.server搭建http文件服务器

    适用场景 只要有python3就可以,windows系统cmd窗口直接敲命令,Linux系统,直接敲命令 step-1 cd cd 到需要搭建服务器的目录 step-2 http.server pyt ...

  5. 《如何进行接口mock测试》

    前言: Mock通常是指:在测试一个对象时,我们构造一些假的对象来模拟与其交互.而这些Mock对象的行为是我们事先设定且符合预期.通过这些Mock对象来测试对象在正常逻辑,异常逻辑或压力情况下工作是否 ...

  6. Python简单算法的实现

    #coding=utf-8 #!/usr/bin/python def GCD(a,b): """ :求两个数的最大公约数 :param a: :param b: :re ...

  7. Spring Cloud Gateway 没有链路信息,我 TM 人傻了(下)

    本系列是 我TM人傻了 系列第五期[捂脸],往期精彩回顾: 升级到Spring 5.3.x之后,GC次数急剧增加,我TM人傻了 这个大表走索引字段查询的 SQL 怎么就成全扫描了,我TM人傻了 获取异 ...

  8. IdentityServer4系列[6]授权码模式

    授权码模式是一种混合模式,是目前功能最完整.流程最严密的授权模式.它主要分为两大步骤:认证和授权.其流程为: 用户访问客户端,客户端将用户导向Identity Server. 用户填写凭证信息向客户端 ...

  9. git批量处理git author和commit

    最近在做自己项目的时候,由于使用了git全局配置的用户名和邮箱,导致自己私人的仓库里面的所有提交记录都是用的公司的邮箱和用户名,于是想批量替换一下. 可以在需要修改的项目的根目录下使用如下命令,进行批 ...

  10. 10.12 LNMP

    yum install nginx php php-fpm mariadb-server php-mysql php.conf server { listen 8000; # pass the PHP ...