Common usage of C + + map in leetcode


Map introduction

Map is a kind of C + +Key value pairContainer, the point is that the data appears in pairs in the map, and the corresponding value can be quickly found by key, that is, key.

Using map

When you program yourself, you need to add the following header file before the code to use map



Map < int, string > my_map; // declare a map
                       //The key value pair type of map can be the basic data type,
                       //It can also be a structure or a class

Map iterator

Like vector, map also has iterator, which is convenient to traverse map

map<int, string>::iterator iter = my_map.begin();

There are also begin, end, rbegin, rend in map. The first two are sequential head and tail iterators, and the last two are reverse.

insert data

There are two ways to insert data in map, one is through subscript insertion, the other is through insert function

my_map[key] = value;
pair<map<int, string>::iterator, bool> Insert_Pair 
= my_map.insert(pair<int, string>(1, "one"));

In addition to the pair data, the parameters in the insert function can also be

map<int, string>::value_type(1, "one")

This is a sub format, but individuals prefer to insert directly through subscripts
The key point to note here is that the results of the above two methods may be different. When the key already exists in the map, it will directly overwrite the previous value through subscript insertion, while insert will not.
We can also see from the code that the return value of the insert function is a pair. The first value of pair is the iterator of the map to be inserted that is the same as the key of the inserted value, while the second value is the result of the insertion. If true is inserted successfully, if false is not.

It is also important not to use subscripts to find out whether there is a key value pair in the map, which will cause new values to be inserted in the map or previous values to be overwritten.

Determine whether the key exists

It is a very important operation for map to judge whether a key exists

Int num = my_map. Count (key); // the return value is 0 or 1, indicating that a
                              //Number of key occurrences
map<int, string>::iterator iter = my_map.find(key);  
                        //The return value is an iterator 
                        //If found, the corresponding key value pair
                        //If it is not found, it is the iterator corresponding to my_map. End()

Delete data

There are three main ways to delete data in map: deleting a key value pair by iterator, deleting some key value pairs by iterator and deleting key value pairs by key

my_map.erase(begin_iter, end_iter);

Using structs in a map

One thing to note here is that the map will be automatically sorted in ascending key order, so if you use the structure as the key or value of the map, you may encounter problems when inserting it. In this case, you need to overload the less than operator. I seldom use this point. I’ll add it if I meet you next time.

Other common functions

My_map. Size(); // returns the number of key value pairs in the map
My_map. Clear(); // clear all key value pairs in the map
My_map. Empty(); // returns whether the map is empty and the return value is bool

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]