当前位置:网站首页>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 )
边栏推荐
- Snowflake won the 2021 annual database
- Reptile learning 3 (winter vacation learning)
- 'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
- SQL greatest() function instance detailed example
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- Dos and path
- Practical dry goods: deploy mini version message queue based on redis6.0
- 20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
- Getting started with window functions
- Test question bank management system - database design [easy to understand]
猜你喜欢
SQL greatest() function instance detailed example
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
Review of week 278 of leetcode II
Day01 preliminary packet capture
Climb Phoenix Mountain on December 19, 2021
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
Reptile learning 3 (winter vacation learning)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
Btrace tells you how to debug online without restarting the JVM
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
随机推荐
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
[ES6] template string: `string`, a new symbol in es2015
Reptile learning winter vacation series (2)
Number and math classes
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
Usage of case when then else end statement
Reptile learning 4 winter vacation learning series (1)
Application of slice
Properties and methods of OS Library
Simple understanding of generics
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
SSH principle and public key authentication
AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
Login operation (for user name and password)
Simple understanding of seesion, cookies, tokens
Test question bank management system - database design [easy to understand]
'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
Force buckle 142 Circular linked list II
Xiaobing · beauty appraisal
Detailed array expansion analysis --- take you step by step analysis