Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

flangazor

Are Map, Hash, not the done thing in C++? If so, why?

This topic is 5710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My strongest language is C++ and in my experience, using hashes and maps are 'not the done thing.' However, in other languages (python, perl, lisp, et al) hashes are much more common. I don't normally see maps often. I am curious why this has become the way things are done. Is this a case of 'C++ is chosen to do tasks that don't normally involve hashing, while Perl, et al are chosen for those tasks' or is it a culture of the language issue? [edited by - flangazor on March 27, 2003 8:48:12 AM]

Share this post


Link to post
Share on other sites
Advertisement
In languages such as Python hashes are comparatively more efficient, so elegant solutions that would have been ignored in C++ can be used.

Much of the power of ''scripting'' languages is personified in something like Ruby''s hash or Python''s dictionary - storage of multiple types, lots of useful operations, and pretty good efficiency.

Doing complex AI in C++ is no fun - I find Ruby far more expressive, partly because of this expressiveness.

I''d agree on the ''choice of tasks'' issue - often scripting languages are used for tasks that are easier with dictionaries.
Indeed:

Task needs a dictionary -> do it in a scripting language
Doing it in a scripting language -> probably use a dictionary anyway

So it goes both ways!

Share this post


Link to post
Share on other sites
I use maps all the time in C++. It''s probably that you''re not looking at the right kinds of applications.

Share this post


Link to post
Share on other sites
quote:
Original post by flangazor
My strongest language is C++ and in my experience, using hashes and maps are ''not the done thing.'' However, in other languages (python, perl, lisp, et al) hashes are much more common. I don''t normally see maps often.

Hashes and maps, as you use them, refer to the same thing. Some literature also refer to them as associative arrays while others call them dictionaries (particularly Python-oriented lit.) Hashing or hashes do exist and are fairly common in C++ applications at a certain level; note the popularity of hash_map and hash_set even though they''re not properly part of the C++ Standard Library.

Recall that C++ has medium-strength, static typing (dynamic typing is only mildly introduced via RTTI mechanisms) while languages like Perl, Python, PHP and Ruby have inherent strong, dynamic typing. This allows them to pack different types together in associative arrays, making dictionaries so much more powerful in those languages. The lack of memory management also makes type-agnostic associative containers so much easier to use in "scripting" languages. Given the language restrictions of C++, associative containers can only store related types (types that can be cast to each other or are derived from the same base class, via a base class pointer). All other methods of coercing containers into storing multiple types offload the responsibility for maintaining type mechanisms onto the programmer, for which you might as well have used embedded Python.

quote:
Is this a case of ''C++ is chosen to do tasks that don''t normally involve hashing, while Perl, et al are chosen for those tasks'' or is it a culture of the language issue?

I think it''s a much more comprehensive valuation, considering a wide variety of language properties and features. But yes, something along those lines - in several instances.

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
the problem is your experience.


petewood, you are usually more helpful than that.

Re: cgoat, what sort of applications do you use? When I think of a problem, I immediately start thinking in terms of lists/vectors/trees. Where I work, its all lists and vectors and trees as well.

I want to break out of my rut (hence, I brought up the topic).

Oluseyi, thanks.

What are some problems where hashes and maps make work easier? I can think of the induction example in ANSI Common Lisp's 15th chapter. Apart from that, I am struggling to think of any examples.

[edited by - flangazor on March 26, 2003 11:11:51 AM]

Share this post


Link to post
Share on other sites
Sorry flangazor, that was short of me.

I find it frustrating when people make broad statements about something which they can''t possibly have experience of. The title of your post was a statement rather than a question - slightly tabloid too.

Whether or not other people use maps a lot in c++ isn''t important is it? If using maps helps you solve your problem then they''re a good solution, if they don''t then they''re not.

Saying they''re ''not the done thing'' gives the impression that there is a culture of resistance to using them which is encouraged and maintained in some way.

Share this post


Link to post
Share on other sites
To be honest, I was under the suspicion that hashes were not very common in the C++ code I have read by other people for very specific reasons. There must have been a reason why ISO said "hashes are not right for the C++ standard library". If it was soley based on the type reasons that Oluseyi cited or based on the fact that hashes have a significant O in their O(1) lookup time, then ok.

If it was something I was not familiar with, I was curious if it was a ''cultural'' reason that was maintained by C++ gurus (Herb Sutter, or Scott Meyers maybe) that hashes are wrong for C++.


--
Can I edit topic titles to add a question mark (?) and remove the tabloid-ness?
--

Share this post


Link to post
Share on other sites
The reason I don''t use hashes or maps much in C++ is mostly because I just don''t think about it. C was my first language and I used static/dynamic arrays for everything (and I didn''t know anything about data structures at all, so using a linked list or a binary tree never entered my mind). Now that the STL has all kinds of data structures, I still find myself using arrays most of the time, though I do use vectors when I need a dynamic array used for anything but a char buffer (when I need a char buffer I usually use the str and mem functions from the C library on it, and they take char pointers and afaik there isnt a way to get to the underlying dynamic array of an stl::vector). I think in terms of arrays when using C++ and only rarely do I think of a solution more easily implemented using a map or hash.

When programming in other languages (like PHP), I have a different mindset (and I don''t usually care about speed), and I''ll use associative arrays more often. In fact, just about every PHP script I''ve ever written uses associative arrays for something.

I think its just a psychological thing. People have different mindsets when using different languages.

Share this post


Link to post
Share on other sites
quote:
Original post by flangazor
the fact that hashes have a significant O in their O(1) lookup time
What "fact" is that? And compared to what? And why would a large constant affect, as hashes are clearly the fastest way of doing some things?

I remember reading from an interview with the original STL designer that he just didn''t have enough time to implement proper hash maps to C++ so he decided to leave them out. And I also recall reading from C++0x plans that they''re going to incorporate hash_map and hash_set to the next standard.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!