当前位置:网站首页>STL map operation
STL map operation
2022-07-23 15:18:00 【joker_ 0030】
#include<iostream>
#include<map>
#include<algorithm>
#include<functional>
using namespace std;
void MapDelete()
{
typedef pair<int, char> in_pair;
map<int, char> db;// Result default key value sorting from small to large .
//map<int, char,greater<int>> db;// The result is modified to sort the key values from large to small .
db.insert(in_pair(1, 'a'));
db.insert(in_pair(2, 'a'));
db.insert(in_pair(3, 'a'));
db.insert(in_pair(4, 'a'));
Iterative output
//map<int, char>::iterator ite = db.begin();
map<int, char,greater<int>>::iterator ite = db.begin();// The result is modified to sort the key values from large to small .
//ite++;
//ite++;
Delete
db.erase(ite);// The delete key value is 3 Value
//db.erase(3);// The delete key value is 3 Value
db.erase(ite, db.end());// The delete key value is 3 Value to the end of the tail .
//ite = db.begin();// After modification, the iterator will fail , So we need to restate .
/*for ( ite ; ite != db.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}*/
// Iterator lookup .
map<int, char>::iterator ite = db.begin();
map<int, char>::iterator ite1 = db.find(2);// lookup 2 This key value , Finding it will return 2 The iterator at the location .
if (db.end() == db.find(6)) // If there is no 6 This key value , be find Return to the last element end()( Not the last key , But next to the actual tail element end()), The operation will crash .
{
cout << "OK" << endl;
}
cout << ite1->first << " " << ite1->second << endl;
cout << db.lower_bound(3)->first << endl;// Returns the parameter key The location of , This position is greater than or equal to key:key<=pos.
cout << db.upper_bound(3)->first << endl;// Returns the parameter key Next position of , This position is greater than key:key<pos.
}
int main()
{
//MapOther();
MapDelete();
system("pause");
return 0;
}
* Key values cannot be modified ( Caused by red and black trees ), The actual value can be modified , Changing the key value will change the whole structure .
边栏推荐
- 【7.16】代码源 -【数组划分】【拆拆】【选数2】【最大公约数】
- MariaDB 数据库升级版本
- 他山之石 | 阿里多模态知识图谱探索与实践
- Safe operation 7.22
- The exclamation point of vscode +tab shortcut key cannot be used, and the solution to the problem of a-soul-live2d plug-in
- Matlab simulation of Turbo code error rate performance
- Linked list review!
- 面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
- 深入理解CAS (自旋锁)
- 解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
猜你喜欢
随机推荐
Blazor quickly realizes Minesweeper
[machine learning basics] unsupervised learning (5) -- generation model
IO流之 字节流 & 字符流
Simulation of synchronization performance of BOC modulation and demodulation based on MATLAB, output tracking curve and identification curve under different lead lag code distance
百度工程师眼中的云原生可观测性追踪技术
【机器学习基础】无监督学习(5)——生成模型
头部姿态估计原理及可视化_loveliuzz的博客-程序员宅基地_头部姿态估计
338. Bit count
解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
VSCode 更新後與tab相關快捷鍵無法使用
Smart headline: smart clothing forum will be held on August 4, and the whole house smart sales will exceed 10billion in 2022
How to realize 485 wireless communication between multiple sensors and Siemens PLC?
7.13WEB安全作业
力扣-单调栈
[heuristic divide and conquer] the inverse idea of heuristic merging
it 农民工的现状和历史
粒子边界碰撞的处理
NVIDIA vid2vid paper reproduction
Leetcode: 17. letter combination of phone number
Exploration and practice of Ali multimodal knowledge atlas









