开发者

Copy hashtable to another hashtable using c++

I am starting with c++ and need to know, what should be the a开发者_运维技巧pproach 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.


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);


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜