473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Saving hash_map keys in a vector

Hi,

Is there a more elegant and/or efficient way of saving the keys from a hash_map in a vector? Perhaps using assign or other algorithms? The problem, of course, is that we are dealing with an iterator of pairs...

Expand|Select|Wrap|Line Numbers
  1. hash_map<string, void *> hashMap;
  2.  
  3. // ...
  4.  
  5. vector<string> keys;
  6. keys.reserve(hashMap.size());
  7.  
  8. hash_map<string, void *>::iterator endIter = hashMap.end();
  9. for (hash_map<string, void *>::iterator iter = hashMap.begin(); iter != endIter; ++iter)
  10. {
  11.     keys.push_back(iter->first);
  12. }
Many thanks!

Paul
Nov 13 '07 #1
6 3020
RRick
463 Expert 256MB
Why do you need to save them at all? You already have them stored in a hash table. One copy is enough.

If you do copy them to a vector, then you have to maintain two different lists of values. This is error prone and best avoided.
Nov 13 '07 #2
RRick,

I'm concerned with two use cases.

First, I need to be able to produce a memento of the map. However, I don't care about the values for this purpose, I only need to remember the keys.

Second, I have a data structure whose underlying implementation (via composition) is a hash_map. I want to keep this data structure as simple as possible, so I don't want to make it (anywhere near) STL compliant and provide iterator access. So I enable the users of the data structure to query the keys that it contains.

Does this make sense to you?

Regards,
Paul
Nov 15 '07 #3
RRick
463 Expert 256MB
Okay, you have a hash map you don't want to make public. That is understandable but you also have some work arounds.

You can allow access to the hash_map as a reference to const hash map. Now, the caller can access the keys and data via const interators, and you don't have to do any extra coding. Please note, that lots of people have issue with this type of access, saying it breaks encapsulation. You'll have to make the decision.

The other choice is to provide the data in some other form. A vector of keys is reasonable, but has its limitations. You now have copy of the keys. What if things change? Who gets rid of the data? You can pass the vector to the app each time it asks or maintain it internally. Once again you get to make the decisions.
Nov 15 '07 #4
Hi Rick,

Many thanks for your reply.

I do indeed feel uneasy about breaking encapsulation.

As far as the vector of keys is concerned, it's far from ideal, but I would make it part of the contract that this is only a snapshot, or a memento (although not in the strict Design Patterns sense of the word). Then I believe it should be OK.

This said, does the code that I pasted in my first message look reasonable to you (provided that we establish that vector<string> is the desired solution).

Once again, thank you for your help,

Paul
Nov 16 '07 #5
RRick
463 Expert 256MB
There's nothing wrong with the code. The issues are with the copies you are making.

For example, is the vector of keys created inside a method? If so, is the vector local to that method? If it is, you must pass back another copy of the vector.

Many times a simple idea has ramifications that go beyond the code needed to generate object. Sometimes, the maintenance of the object is more complicated that its creation.
Nov 16 '07 #6
Thanks a lot for your advice, RRick.

I'm hoping that if I create the vector within a method and then return this vector, the additional copy will be removed by most modern compilers (return value optimisation).

Is this generally the case in your experience?

Many thanks,
Paul
Nov 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sabrina | last post by:
Can someone help? I have been trying to get the hash_map in C++ for .NET to work with strings and const char*. I am using the const char* as the key and a pointer to another class as the data...
5
by: Charles Herman | last post by:
I have the folowing program: #include <iostream> #include <ext/hash_map> using namespace std; using namespace __gnu_cxx; typedef hash_map<char*, long, hash<char*> > char_hash_t;
3
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own...
3
by: Michael | last post by:
Hi, I have a map and I want to save it to disk. What is the most efficient method of doing this? As i understand it, maps are stored as a binary tree. Is it balanced or is this implementation...
1
by: anjangoswami06 | last post by:
Hi, I am interested to know how it is possible to update the data type with "insert" with stl hash_map. Suppose we have, hash_map<const char *, MyDataType, hash_compare<const char*,...
1
by: Amit Bhatia | last post by:
Hi, I have defined something like the following in tree.h file: //everything else including using namespace __gnu_cxx; typedef hash_map<pair<int,int>, Qd_Node, Qd_Node_HasherLoc_Tree;...
1
by: joseysaac | last post by:
i have this code in a archive called ffont.h #ifndef FFONT_H_FILE #define FFONT_H_FILE #include "FBase.h" #include "FShape.h" #include <vector> #include <hash_map>
5
by: devdude | last post by:
Hi, I have the need to take a snapshot of a hash_map during execution (actually transform it to a vector). This map is a shared resource and therefore must be locked prior to any read/write...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.