tags:

views:

298

answers:

4

I am starting with c++ and need to know, what should be the approach to copy one hashtable to another hashtable in C++?

We can easily do this in java using: HashMap copyOfOriginal=new HashMap(original);

But what about C++? How should I go about it?

UPDATE

Well, I am doing it at a very basic level,perhaps the java example was a wrong one to give. This is what I am trying to implement using C++:

I have this hash array and each element of the array is the head of a linked list. Which has it's individual nodes (data and next pointer).

And now, I need to copy the complete hash array and the linked list each node is pointing to.

+2  A: 

Whichever hashmap you're using, I'm sure it has a copy-constructor and possibly operator=.

hashmap_type newMap = oldMap; // copies

And that's it. C++ has no standard hash map, though.

GMan
@stefanB, no, you're wrong. std::unordered_map is being added in C++0x, but there is no hash map class in ISO C++ 1998 (w/2003 ammendments).
Michael Aaron Safyan
The STL is not C++. C++ has no standard hash map, although `std::unordered_map` will fill that void once C++0x is finalized.
Dennis Zickefoose
@Dennis, the STL is a part of the C++ language... the standard library is just as much a part of C++ as is the language syntax and semantics, and is adopted by the same standardization process.
Michael Aaron Safyan
@Michael Aaron Safyan: SGI's original STL is _NOT_ part of the C++ language. SGI's original STL contains hash_map. The standard does not.
Billy ONeal
@Billy, SGI's implementation of STL is not "the STL". The STL is whatever is defined to be the STL by the ISO C++ standards committee. Maybe you're confusing me with stefanB?
Michael Aaron Safyan
The C++ standard makes no reference to any standard template library. It defines a standard library. To say "The STL" is a part of the C++ language is simply wrong. --Although yes, I did get you and stefanB confused :-)
Dennis Zickefoose
sgi has `operator=` sgi.com/tech/stl/hash_map.html, microsoft does not list it msdn.microsoft.com/en-us/library/… (but that might be just microsoft way of doing things ...) EDIT: I was talking about `operator=` not the `hash_map` support sorry for the confusion
stefanB
@GMan Are you losing your grip :-) There's no operator=() in the code in your answer!
anon
@Neil: Bah. :P Fix'd.
GMan
+2  A: 

In C++ you would use either the copy constructor or simple assignment (with values) to perform this.

For example

std::map<int,string> map1 = CreateTheMap();
std::map<int,string> map2 = map1; 
std::map<int,string> map3(map1);
JaredPar
std::map is not a hash map, though.
Michael Aaron Safyan
Okay, replace with `unordered_map`.
Billy ONeal
@Michael, this appears to be more of a general question. I don't think the OP was looking for an example that included a C++ type that matches Java semantics exactly
JaredPar
@JaredPar: There is no C++ type that matches Java semantics exactly. Any question asking for "Type X which matches language semantics of language Y in language Z" almost always results in "It does not exist". Different languages have different idioms. One should use idioms appropriate to that language.
Billy ONeal
@JaredPar: Oh, BTW, +1 :)
Billy ONeal
@Billy, while that may be true, you aren't going to get the same syntax or method names, it is reasonable to expect a container with similar behavior and the same asymptotic complexity. The std::map has a very different runtime complexity than boost::unordered_map (and, very soon, std::unordered_map) and Java's HashMap. It is very reasonable to want an O(1) insertion and O(1) lookup.
Michael Aaron Safyan
@Michael Aaron Safyan: There is a reason I upvoted the answer....
Billy ONeal
+1  A: 
Michael Aaron Safyan
To be fair, the reason it was there was from Dinkumware's shipment of the STL, which includes hash_map, because it existed in the original STL from SGI.
Billy ONeal
A: 

I am afraid, somehow, that you are using a custom HashMap class, since you speak about its implementation details.

In C++, when it comes to copying a class, there is a special purpose Copy Constructor, which syntax looks like:

class Foo
{
public:
  Foo(); // regular constructor

  Foo(const Foo& rhs); // copy constructor
};

It can be invoked with either syntax:

Foo copy(original);
Foo copy2 = original;

Now, if you HashMap does not provide a copy constructor, my first suggestion is to switch to an existing implementation, like boost::unordered_map or if available std::hash_map, std::tr1::hash_map or std::tr1::unordered_map. The reasons there are so may std:: possibilities is that many STL features a hash_map long before it was standardized. unordered_map is here to stay though, and the boost one too.

If you can't switch you are bound to implement the copy operation somehow.

Matthieu M.