C++ map操作——插入、查找、遍历
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操作——插入、查找、遍历的更多相关文章
- C++(十一)— map的插入、查找、删除
标准库map类型是一种以键-值(key-value)存储的数据类型. map是STL的一个关联容器.它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值 ...
- 如何遍历Map操作总结
Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "123"); ...
- Java集合list,map,set区别及遍历
1.1 List.Set.Map基本区别 1.List,Set都是继承Collection接口,Map不是. 2.List:LinkedList.ArrayList.Vector Set :HashS ...
- 如何优雅的传递 stl 容器作为函数参数来实现元素插入和遍历?
问题背景 开始正文之前,做一些背景铺垫,方便读者了解我的工程需求.我的项目是一个客户端消息分发中心,在连接上消息后台后,后台会不定时的给我推送一些消息,我再将它们转发给本机的其它桌面产品去做显示.后台 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第 ...
- c/c++ 标准库 map set 插入
标准库 map set 插入 一,插入操作 有map如下: map<string, size_t> cnt; 插入方法: 插入操作种类 功能描述 cnt.insert({"abc ...
- 小白鼠排队(map容器插入数据的四种方法)
题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...
- JavaScript中的Map、Set及其遍历
Map Map是一组键值对的结构,具有极快的查找速度. Map的定义. //空map设值key-value var m = new Map(); m.set("XiaoMing", ...
随机推荐
- 重学VUE——vue 常用指令有哪些?
一.什么是指令? 在 vue 中,指令以 v- 开头,是一种特殊的自定义行间属性.指令属性的预期值是一个表达式,指令的职责就是:表达式的值改变时,相应地将某些行为应用到DOM上.只有v-for是一个类 ...
- 怎样在Linux中查看apache是用那个httpd.conf
第一步:找到apache启动命令: [root@WAPBJ01 ~]# ps -ef|grep httpdroot 10575 1 0 19:45 ? 00:00:03 ...
- Docker系列(3)- 配置阿里云镜像加速
step-1 登录阿里云找到容器服务 step-2 找到镜像加速地址 step-3 配置使用 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon ...
- CSS写一个缺角的div和菱形
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- centos7安装sonarqube与使用
https://www.cnblogs.com/mascot1/p/11179767.html https://blog.csdn.net/superbfly/article/details/1039 ...
- MySQL 服务无法启动。 服务没有报告任何错误。 请键入 NET HELPMSG 3534 以获得更多的帮助。
第一步 有一部分人成功了在安装根目录创建data文件夹 第二步 有一部分人成功了D:\Program Files\mysql\bin>mysqld initialize 第三步 这步之后我安装成 ...
- python读取文件编码转换问题
encode(编码) decode(解码) encoding(编码格式) #-*- coding:utf-8 -*- import chardet #用于查看编码 with open(&quo ...
- torch的下载及安装
Pytorch官网:https://pytorch.org/ 安装的时候如果选择在官网上下载安装的话下载会很慢,试过梯子也是很慢,此处提供一种快速安装的方法. 1.由于我是window系统,我选择了国 ...
- 【图像处理】基于OpenCV实现图像直方图的原理
背景 图像的直方图是衡量图像像素分布的一种方式,可以通过分析像素分布,使用直方图均衡化对图像进行优化,让图像变的清晰. opencv官方对图像直方图的定义如下: 直方图是图像中像素强度分布的图形表达方 ...
- The Data Way Vol.1|风口下的开源市场:如何看待开源与商业的关系?
关于「The Data Way」 「The Data Way」是由 SphereEx 公司出品的一档播客节目.这里有开源.数据.技术的故事,同时我们关注开发者的工作日常,也讨论开发者的生活日常:我们聚 ...