当前位置:网站首页>Note: unordered_ Understanding and use of map
Note: unordered_ Understanding and use of map
2022-04-23 05:27:00 【What’smean】
unordered_map
unordered_map Containers , The literal translation is " disorder map Containers " It means . So-called “ disorder ”, refer to unordered_map Containers don't look like map Sort the stored data like a container . let me put it another way ,unordered_map Container and map Containers are only a little different , namely map The data stored in the container is orderly , and unordered_map The container is unordered .
In particular ,unordered_map Container and map The container is the same , Pair with key value (pair type ) In the form of data storage , The keys of each stored key value pair are different from each other and cannot be modified . But because of unordered_map The bottom layer of the container adopts the hash table storage structure , The structure itself does not have the function of sorting data , Therefore, this container will not sort the stored key value pairs by itself .
Example
function myAtoi(string s) The algorithm is as follows :
Read in strings and discard useless leading spaces
Check the next character ( Suppose you haven't reached the end of the character yet ) Positive or negative , Read the character ( If there is ). Determine whether the final result is negative or positive . If neither exists , Suppose the result is positive .
Read in the next character , Until you reach the next non numeric character or the end of the input . The rest of the string will be ignored .
Convert the numbers read in the previous steps into integers ( namely ,"123" -> 123, "0032" -> 32). If you don't read in the numbers , Then the integer is 0 . Change the symbol if necessary ( From step 2 Start ).
If the number of integers exceeds 32 Bit signed integer range [−231, 231 − 1] , You need to truncate this integer , Keep it in this range . say concretely , Less than −231 The integer of should be fixed to −231 , Greater than 231 − 1 The integer of should be fixed to 231 − 1 .
Returns an integer as the final result .
Be careful :The white space character in this question only includes the space character ' ' .
Except for the leading space or the rest of the string after the number , Do not ignore Any other character .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/string-to-integer-atoi
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Explain
class Automaton {
string state = "start";// Status word
unordered_map<string, vector<string>> table = {// Definition unordered_map( Pay attention to key value Yes , among value by string Vector of type )
{"start", {"start", "signed", "in_number", "end"}},
{"signed", {"end", "end", "in_number", "end"}},
{"in_number", {"end", "end", "in_number", "end"}},
{"end", {"end", "end", "end", "end"}}
};
int get_col(char c) {// Choose different values according to different characters , namely map The corresponding subscript in the vector in
if (isspace(c)) return 0;// Judge whether it is a space
if (c == '+' or c == '-') return 1;
if (isdigit(c)) return 2;// Is it a number
return 3;
}
public:
int sign = 1;
long long ans = 0;
void get(char c) {
state = table[state][get_col(c)];//state The corresponding key ,get_col(c) Corresponding value vector coordinates
if (state == "in_number") {
ans = ans * 10 + c - '0';
ans = sign == 1 ? min(ans, (long long)INT_MAX) : min(ans, -(long long)INT_MIN);// Judge whether it is beyond the scope of expression
}
else if (state == "signed")// Select symbol
sign = c == '+' ? 1 : -1;
}
};
class Solution {
public:
int myAtoi(string str) {
Automaton automaton;
for (char c : str)
automaton.get(c);
return automaton.sign * automaton.ans;
}
};
author :LeetCode-Solution
link :https://leetcode-cn.com/problems/string-to-integer-atoi/solution/zi-fu-chuan-zhuan-huan-zheng-shu-atoi-by-leetcode-/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
版权声明
本文为[What’smean]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220544124733.html
边栏推荐
- 2021-11-01
- Redis in node -- ioredis
- The 2021 IT industry project management survey report was released!
- Quick app bottom navigation bar
- Low code and no code considerations
- Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)
- 4 most common automated test challenges and Countermeasures
- 修仙真实世界与游戏世界
- 2021-09-28
- 点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
猜你喜欢
Using PHP post temporary file mechanism to upload arbitrary files
After NPM was upgraded, there was a lot of panic
Box collapse and margin collapse
varnish入门
How to set the initial value of El input number to null
Create process memory management copy_ Mm - processes and threads (IX)
开源规则引擎——ice:致力于解决灵活繁复的硬编码问题
Branch and loop statements
Blender程序化地形制作
Create cells through JS (while loop)
随机推荐
Call the interface to get the time
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
Box collapse and margin collapse
To understand Devops, you must read these ten books!
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
Three methods of list rendering
2021-11-01
Why can't V-IF and V-for be used together
Knowledge of egg testing -- mock, Supertest, coffee
即将毕业的大学生找技术开发工作的焦虑根源
相机成像+单应性变换+相机标定+立体校正
日志简介和构建web应用
Necessity of selenium preloading cookies
Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)
2021-10-08
selenium预先加载cookie的必要性
what is wifi6?
Implementation of resnet-34 CNN with kears
CPT 104_TTL 09
The 2021 IT industry project management survey report was released!