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", ...
随机推荐
- Math.round真的是四舍五入吗?我不这么认为
public static long round(double a) 返回最接近参数的 long.结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 long 类型.换句话 ...
- PHP中环境变量的操作
在 PHP 中,我们可以通过 phpinfo() 查看到当前系统中的环境变量信息(Environment).在代码中,我们也可以通过两个函数,查看和修改相应的环境变量信息. getenv() 获取环境 ...
- vue报错 Uncaught TypeError: Cannot read property of null
有可能是点击a标签,但是a标签有click事件,未阻止默认事件导致报错,开始都看不出来是什么错误
- mogoose 创建数据库并增删改查
下载 npm i mongoose -s 连接数据库 const mongoose = require("mongoose"); mongoose.connect(dbURL); ...
- Windows系统如何找到占用端口的进程并杀掉
1.先建立用户环境变量:C\WINDOWS/system32 2.输入:cmd,打开命令控制台,然后输入ipconfig 3.再输入:netstat -ano(可以找到所有的进程连接端口及对应PID) ...
- 停止:service jenkins stop,提示:Failed to stop jenkins.service: Unit jenkins.service not loaded.
uni@uni-virtual-machine:~$ service jenkins stop Failed to stop jenkins.service: Unit jenkins.service ...
- P7736-[NOI2021]路径交点【LGV引理】
正题 题目链接:https://www.luogu.com.cn/problem/P7736 题目大意 有\(k\)层的图,第\(i\)层有\(n_i\)个点,每层的点从上到下排列,层从左到右排列.再 ...
- 学会了这些英文单词,妈妈再也不用担心我学不会Python
前言 很多转行或刚入行做测试的小伙伴学习Python时,经常会问一句话:我英语不好能不能学会代码. 答案是:肯定的!你如果英语好学开发语言肯定要比不会英语的小伙伴学起来.当代码报错时全是英文,毕竟 ...
- @ConfigurationProperties与@Value的区别
1. 首先我们来看每一个的具体使用 1. ConfigurationProperties 1. application.yml文件中: person: lastName: xj age: 18 bos ...
- 通用JS七
instanceof 在原型链上寻找这个属性的定义 match 正则匹配字符串 Symbol() Symbol()函数不能用作构造函数,与new关键字一起使用.这样做是为了避免创建符号包装对象,像使用 ...