What is the best way to use a HashMap in C

0 votes
I'm aware that STL offers a HashMap API, but I can't locate any excellent and detailed documentation with examples.

Any good examples would be much appreciated.
Jun 9, 2022 in C++ by Nicholas
• 7,760 points
459 views

1 answer to this question.

0 votes

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library. 

The items in an ordered map are sorted by key, and insert and access are in O (log n). 

For ordered maps, the standard library often use red black trees. 

However, this is only an implementation detail. 

Insert and access are in O in an unordered map (1). 

It is simply another term for a hashtable.

An illustration using (ordered) std::map:

#include <map>
#include <iostream>
#include <cassert>

int main(int argc, char **argv)
{
  std::map<std::string, int> m;
  m["hello"] = 23;
  // check if key is present
  if (m.find("world") != m.end())
    std::cout << "map contains key world!\n";
  // retrieve
  std::cout << m["hello"] << '\n';
  std::map<std::string, int>::iterator i = m.find("hello");
  assert(i != m.end());
  std::cout << "Key: " << i->first << " Value: " << i->second << '\n';
  return 0;
}

Output:

23
Key: hello Value: 23

If you require ordering in your container and don't mind the O(log n) runtime, use std::map.

answered Jun 10, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What is the fastest way to transpose a matrix in C++?

I have a reasonably large matrix that I need to transpose.  Assume, for example, that my matrix is a b c d e f g h ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
509 views
0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
503 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
287 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
387 views
0 votes
0 answers

What is the best way to use a HashMap in C++?

Can someone recommend me some good documentation ...READ MORE

May 19, 2022 in Others by Kichu
• 19,050 points
257 views
0 votes
2 answers

How is hashmap different from hashtable?

HashMap and HashTable: 1) Hashtable and Hashmap implement ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
5,970 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,090 points
485 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,090 points
2,117 views
0 votes
1 answer

Is there an easy way to make a min heap in C++?

Use make heap() and its buddies from algorithm>, or priority queue from queue>.  Make heap and friends are used by priority queue. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
658 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
292 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP