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.
Well, what hash table implementation are you using? There is no hash table provided by the current version of ISO C++. That said, if your hash table class does not make
operator=and its copy constructor private, then it would be a reasonable assumption that both will behave as expected. If not, I’d consider it a bug.As an aside std::unordered_map is being added in ISO C++ 2010, but ISO C++ 1998 and ISO C++ 1998 with the 2003 amendments do not have a hash map container. Microsoft provided a non-standard “std::hash_map”, which they should never have placed in the “std::” namespace. They have since moved it to “stdext::” (which is good news). Some other vendors copied MSFT to make their compilers compatible.
If you are anxious to use a hash table implementation right away, then use boost::unordered_map from the Boost C++ Libraries. The Boost C++ Libraries are open source, very popular, and high quality.
EDIT
Based on your updated question, you will need to create your own copy constructor, a swap function, and an implementation of operator= in order to do this. Usually operator= is trivial once you have swap and a copy constructor in place. Here is a sketch of how you would do this:
template<typename T> HashTable<T>::HashTable(const HashTable<T>& o) { // pseudo code: // initialize as in HashTable<T>::HashTable() // for each key/value pair in o: // insert that key/value pair into this instance // // NOTE: // if your hash table is sized so that the number of // elements is a prime number, you can do better // than the pseudo-code given above, but otherwise // copying element by element is the way to go. // // BEGIN YOUR CODE // ... // END YOUR CODE } template<typename T> HashTable<T>& HashTable<T>::swap(HashTable<T>& o) { // Swap data pointers T* datatmp = _data; _data = o._data; o._data = datatmp; // Swap capacity size_t captmp = _capacity; _capacity = o._capacity; o._capacity = captmp; // Swap other info // ... // Report self return *this; }template<typename T> HashTable<T>& HashTable<T>::operator=(const HashTable<T>& o) { HashTable<T> cpy(o); return swap(cpy); }You will have to take the signatures from the above and add them to your declaration. I should also point out that one reason that
operator=tends to be implemented in terms of swap is that, not only is it very simple to do and having a swap function makes your code very fast when that operation is needed, but also for the purposes of exception safety… your swap should pretty much never fail, but copy construction might… so if the copy construction throws an exception, you haven’t thrown the object’s state to hell.