1

Topic: CookBook: unordered_map

All greetings!
At such question
There is at me unordered_map <string, something> um _ in a class.
Objects of this class in real-time are interrogated um _ [key]
The data in um _ the order 3-15, well 100 maximum.
Objects of a class can be much, 1-100, inquiry becomes on the timer, the purpose - to avoid logs.
While a question only on operation with .
2 approaches come to a head, with .. Productivity.
1.

Smth a = um _ [key];
if (a)
{
//Do
}

In this case, if in um_ there is nothing  to "key", the empty object there will be got,  expands, and plus in the code one check on nullptr is necessary;
2.

try
{
Smth a = um_. at (key);
//Do
}
catch (...)

Here check is not present,  does not expand, but walk  on a subject  at usage of exceptions.
Probably there are still variants?
How it is better to make, within the limits of the task?

2

Re: CookBook: unordered_map

1. It is reduced in all cases to _Try_emplace which works through find, and there all the same there is a check on end (), therefore it is possible to use at once simply find ()

3

Re: CookBook: unordered_map

CEMb wrote:

All greetings!
At such question
There is at me unordered_map <string, something> um _ in a class.
Objects of this class in real-time are interrogated um _ [key]
The data in um _ the order 3-15, well 100 maximum.
Objects of a class can be much, 1-100, inquiry becomes on the timer, the purpose - to avoid logs.
While a question only on operation with .
2 approaches come to a head, with .. Productivity.
1.

Smth a = um _ [key];
if (a)
{
//Do
}

In this case, if in um_ there is nothing  to "key", the empty object there will be got,  expands, and plus in the code one check on nullptr is necessary;
2.

try
{
Smth a = um_. at (key);
//Do
}
catch (...)

Here check is not present,  does not expand, but walk  on a subject  at usage of exceptions.
Probably there are still variants?
How it is better to make, within the limits of the task?

Crazy question, but generally if speed is necessary to you, first of all it is necessary to get rid from operator [] and to use access methods to values in the functional notations. I.e.  . at () find () and so on.

4

Re: CookBook: unordered_map

MasterZiv wrote:

it is passed...
Crazy question, but generally if speed is necessary to you, first of all it is necessary to get rid from operator [] and to use access methods to values in the functional notations. I.e.  . at () find () and so on.

And than operators are bad?

5

Re: CookBook: unordered_map

semen.s.semen wrote:

And than operators are bad?

Semantics at them difficult...
https://en.cppreference.com/w/cpp/conta … perator_at

6

Re: CookBook: unordered_map

Key  not to do

7

Re: CookBook: unordered_map

Siemargl wrote:

  then

the key will not do a key hash from string. To do not  it it is not convenient, they are described in  files , and to write on storage this heap of digits difficult even to me.

8

Re: CookBook: unordered_map

CEMb wrote:

hash from string

though, I suspect, it already there indeed

9

Re: CookBook: unordered_map

CEMb;
In respect of productivity it is very good to know a desirable amount of keys and to select under them a necessary place in advance - reserve then in an ideal there will be no table reorganization
Concerning usage in the form of a cache, MasterZiv wrote
The variant with exceptions quite live but where be in a python (there itself used, worked faster, specially checked)
well and still such , unordered_map it is impossible use on addition at actions with a content which can to carry threat - for example, on the server at  the transferred parameters

10

Re: CookBook: unordered_map

CEMb;
If them 3-15 probably it is better to store them in a normal vector (or similar ).
Because in  they can be scattered on a heap and search will be with speed of the bus. Well and lines to replace with special objects which would store the data not on a heap, and there where they are allocated.

11

Re: CookBook: unordered_map

kealon (Ruslan) wrote:

it is good to know a desirable amount of keys

alas, there random.

kealon (Ruslan) wrote:

then in an ideal there will be no table reorganization

and unless unordered_map is rebuilt?

kealon (Ruslan) wrote:

a variant with exceptions quite live

and why say, what exceptions slowly work? Many times came across disputes at forums. Or exceptions too should be known how to cook correctly?

alex_k wrote:

if them 3-15 them I probably am better to store in a normal vector

took unordered_map from reasons of fast search in a key. In case of a vector it is necessary to do it by search.

alex_k wrote:

because in  they can be scattered on a heap and search will be with speed of the bus.

I tested in due time search in this , at it bright enough indexes at the huge sizes.

12

Re: CookBook: unordered_map

CEMb wrote:

it is passed...
And unless unordered_map it is rebuilt?

it is finite

CEMb wrote:

it is passed...
And why say, what exceptions slowly work? Many times came across disputes at forums. Or exceptions too should be known how to cook correctly?

There all is very turbid, especially on x64.
But for python  it is pure arithmetics because of language singularities:
To cause something it it is necessary from this  to get variables a variable, and then to get from it is 2 operations
And to check up and get already quits 4
I.e. if to start with usage singularities , you will be guaranteed to lose