What is a real example of what you represent with a hash?

I'm just trying to figure out when you need to use a hash, and when it's better to use an array. What real world object will the hash represent, say, in the case of strings?

+3
source share
12 answers

I believe that sometimes a hash is referred to as a “dictionary”, and I think this is a good example in itself. If you want to find the definition of a word, then it's nice to do something like:

definition['pernicious']

Instead of trying to figure out the correct numerical index to be stored in the file.

This answer assumes that with a "hash" you basically just reference an associative array.

+14
source

, . , , , , . - . , , Dictionary, ( , ) O (1). O (logn), .

, (hashmaps), (hashsets) ..

( ).

+5

- = , value = .

( ). "" ( "C" ).

+2

, , 1 1.

, :

" " = > " +"

"Jacob Jenkens" = > "C"

..

+1

, - - , , - "".

, - , , - - , "" , , 1:1 -, , , .

+1

. " " , / .

0

. , . , , . . ( .).

0

"" .

|

- .

.

0

, , .

0

. , (, URL-, -). , , , - URL. - - .

0

, , - , .

, , , . , ( , ..)

- . , ​​ ( ). , , . .

0
source

(php code)

$david        = new stdclass();
$david->name  = "david";
$david->age   = 12;
$david->id    = 1;
$david->title = "manager";

$joe        = new stdclass();
$joe->name  = "joe";
$joe->age   = 17;
$joe->id    = 2;
$joe->title = "employee";

// option 1: lets put users by index
$users[] = $david;
$users[] = $joe;

// option 2: lets put users by title
$users[$david->title] = $david;
$users[$joe->title]   = $joe;

Now the question is: who is the manager? Answer:

$users["manager"] 
0
source

Source: https://habr.com/ru/post/1705047/


All Articles