C ++ and map of unordered_map

Reprinted from Los Valley [ https://www.luogu.org/blog/yihan/unordered ] ( https://www.luogu.org/blog/yihan/unordered )

what is this?

We know that there are some useful container in c ++ 11, including two (three) very practical container: unordered_map & unordered_set / unordered_multiset

Its implementation and operation map & set / multiset similar, but we know the map and set implementation is  O (logN) O ( L O G n- ) for realization of a map, in which red-black tree implementation is built. Most of the time has been very good, but often there will be some problems in less than an ordered sequence (we know the internal set is ordered), but need more speed. This time you can choose to play hash. But for yijan patients with advanced cancer naturally want this lazy practical unordered_map & unordered_multiset achieve  O (1) O ( 1 ) query.

If you will use, you can skip this section

The first is defined. If in c ++ 11 environment, it is easy to write:

#include<unordered_set>
#include<unordered_map> #include<iostream> using namespace std; unordered_set<int> S1; unordered_multiset<int> S2; unordered_map<int,int> M; int main(){ S1.insert(6); S2.insert(7); M[233333] = 666666; cout << M[233333]; }

But we NOIP Series race is obviously not of AIDS porcelain c ++ 11! How to do?

That certainly is a clever but useless! :(It is said that there is noquestionable)

#include<tr1/unordered_set>
#include<tr1/unordered_map> #include<iostream> using namespace std; using namespace tr1; unordered_set<int> S1; unordered_multiset<int> S2; unordered_map<int,int> M; int main(){ S1.insert(6); S2.insert(7); M[233333] = 666666; cout << M[233333]; }

Then you can use in a non-11 environment! (Such as luoguide choose not vote for c ++ 11 will be able to run!)

Well, said so much, following entered ((

Now we want to! Card out unordered_map!

But to say simply, how cards fall? First, we know that, as long as the hash function, if multiple collisions, the natural rate of  O (the n-) -> O (the n-^ 2) O ( the n- ) - > O ( the n- 2 )

If you are not sensitive to the discovery process can skip this paragraph

First of all, let's go unorderedmap source file: the On GitHub

We have to know the realization of the principle of the thing, which we can see this sentence, apparently called the hashtable  _Mod_range_Hashing and  _Prime_rehash_policyfrom here we can know about this thing is the implementation process: first hash the data, and then put the hash value modulo the unordered_map.

To _Prime_rehash_policyexplore ( hashtable_c ++ 0x.cc ) we can find an array: __prime_list. At this point you can turn and find out which part of the implementation: When the map is too large, it will resize itself. This put aside when we find this prime number table.

And then it opened ( hashtable-aux.cc ) which you can see the array of!

Well, we now find this array, and master the basic implementation. But we should find out the built-in hash realize what it was. In fact, the use of std::hashwritten in cppreference in:

Unordered associative containers std :: unordered_set, std :: unordered_multiset, std :: unordered_map, std :: unordered_multimap to the special template std :: hash into default hash function.

Here, we can be snapped out of it. Not all prime numbers can do this, and only one or two exceptional. Because bloggers too lazy to take the time to look for this prime number, the following references cf Bowen sentence:

for gcc 6 or earlier, 126271 does the job, and for gcc 7 or later, 107897 will work.

We now use the compiler more than 126,271 6or earlier it is a very appropriate prime number.

Really stuck out a code for

luogu have a problem P1102 number of AB This is a common map can ac water problem. Let's compare unordered_set & unordered_map

This is part AC Code:code

Then in the figure, it is the use of the above map, using the following unordered_map

res

It can be seen after regardless of space and time are unordered outstanding.

Hack It!

We use prime numbers that card off in front of said code to do the input file. 1 to a digital input of 200,000 times 126,271. Respectively, and then use the map unordered_map to run. To see what are the results? (In other title oj created, the speed should be similar)
test data download: input_file

map

unordered

Yes, you read right, a full run 4s!

Not hack?

Write a hash function!

Of course, I believe that most of the time is not going to make things difficult for someone hack your unordered_map of. . Unless cf game. . . (Ccf making this data that I have nothing way)

Specific write their own hash function can be reference materials I made myself. This is no longer a burden.

to sum up

unordered_map / set is indeed a good thing!

If noip found in complexity yes,

Try to use the map

After all, and consequently can ccf card (dead a shortest path fake algorithm)

Do not forget to hack cf fate of others oh

About stuck out a hash reference cf article:  Neal's Blog (blogger has received a license from reproduced)

There are many natural noip examination room on this can be used toCheat pointsWith STL containers. About sustainable balanced tree can also refer to a solution to a problem I can achieve lasting balance tree forty lines (board title MLE)

Of course, pbds party must think hash this thing called directly ah QWQ that I have no other way. .

Guess you like

Origin www.cnblogs.com/tldr/p/11364781.html