当前位置:网站首页>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 21
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
- 3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
- A few words explain redis cache penetration, breakdown, avalanche, and redis sentinel
- AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
- OSI seven layer reference model
- VPS installation virtualmin panel
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
- iptables导致Heartbeat脑裂
猜你喜欢

Btrace tells you how to debug online without restarting the JVM
![[solve the error of this pointing in the applet] SetData of undefined](/img/19/c34008fbbe1175baac2ab69eb26e05.jpg)
[solve the error of this pointing in the applet] SetData of undefined

Serialization oriented - pickle library, JSON Library
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8](/img/16/33f5623625ba817e6e022b5cb7ff5d.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18](/img/1a/94ef8be5c06c2d1c52fc8ce7f03ea7.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18

Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology

Enter the smart Park, and change begins here

Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing

TCP slicing and PSH understanding
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6](/img/38/51797fcdb57159b48d0e0a72eeb580.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
随机推荐
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
SSH原理和公钥认证
Clion configuration of opencv
OSI seven layer model & unit
[Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
2021-10-20
03_ Armv8 instruction set introduction load and store instructions
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
Solaris 10网络服务
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
[ES6] template string: `string`, a new symbol in es2015
2021-11-02
Using terminal connection in different modes of virtual machine
Simple understanding of string
Common tips
Four sorts: bubble, select, insert, count