当前位置:网站首页>Map container
Map container
2022-07-04 11:55:00 【Rivers overflow】
#include<map>
using std::map;
1.map The definition of
map<typename1,typename2> name;typename1 As the key (key),keyname2 Value (value)
2.map Access to elements within a container
(1) The subscript access
Note that a key can only correspond to one value
map<char,int> mp;
mp['a']=100;
mp['a']=120;
cout<<mp['a'];
// Output 120(2) Iterators access
Iterator definitions and others STL identical
map<typename1,typename2>::iterator it;Use and others STL There's a difference , Because iterators need to be able to access keys and values at the same time , Use ->first and ->second Access keys and values respectively
Be careful :map According to key Automatically sort the size of , Be similar to set
#include<bits/stdc++.h>
using namespace std;
signed main(void)
{
map<char,int> mp;
mp['c']=1;
mp['b']=2;
mp['a']=3;
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n",it->first,it->second);
}
return 0;
}
/*
The output is ( Automatic sorting )
a 3
b 2
c 1
*/3.map Common function analysis
(1)find()
find(key) Return key is key The iterator of the map of , The time complexity is O(logn),n by map Number of mappings in
#include<bits/stdc++.h>
using namespace std;
signed main()
{
map<char,int> mp;
mp['b']=2;
mp['c']=1;
mp['a']=3;
map<char,int>::iterator it=mp.find('b');
printf("%c %d\n",it->first,it->second);
return 0;
}(2)erase()
1.erase(it) it Iterator for the element to be deleted , Time complexity O(1)
2.erase(key) key Is the key of the element to be deleted , The time complexity is O(logn),n by map Number of internal elements
3.erase(first,last) Left closed right away ,[first,last), The time complexity is last-first
(3)size()
Return mapping logarithm ,O(1)
(4)clear()
Empty elements ,O(n),n Is the number of elements
4.map Common use
multimap( A key corresponds to multiple values )、unordered_map( Don't order )
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
- Recommend a cool geospatial data visualization tool with low code
- Configure SSH key to realize login free
- Iframe to only show a certain part of the page
- Simple understanding of generics
- QQ get group link, QR code
- [ES6] template string: `string`, a new symbol in es2015
- How to deal with the relationship between colleagues
- Dos and path
- Xshell's ssh server rejected the password, failed to skip publickey authentication, and did not register with the server
猜你喜欢

Games101 Lesson 8 shading 2 Notes

Notes on writing test points in mind mapping
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16](/img/c3/f3746b161012acc3751b2bd0b8f663.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5

Practical dry goods: deploy mini version message queue based on redis6.0

Simple understanding of seesion, cookies, tokens

Introduction to Lichuang EDA

The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9](/img/ed/0edff23fbd3880bc6c9dabd31755ac.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9

Day01 preliminary packet capture
随机推荐
If function in SQL
Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
(August 9, 2021) example exercise of air quality index calculation (I)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 10
OSI seven layer reference model
In 2022, financial products are not guaranteed?
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
Simple understanding of seesion, cookies, tokens
三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
Four sorts: bubble, select, insert, count
Recommend a cool geospatial data visualization tool with low code
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
Automatic translation between Chinese and English
Reptile learning 4 winter vacation learning series (1)
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
Serialization oriented - pickle library, JSON Library
Realize cross tenant Vnet connection through azure virtual Wan
Reptile learning 4 winter vacation series (3)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7