Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:16:48+00:00 2026-05-14T04:16:48+00:00

I am starting with c++ and need to know, what should be the approach

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:16:49+00:00Added an answer on May 14, 2026 at 4:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Rails app is starting to need complicated queries. Should I just start using
I know this should be fairly easy but I can't quite figure it out
I need to access a file larger than 2gb using C. During one run
I realize this is probably a fundamental thing I should know but I am
Im starting with XNA and i need an advice about the following. I have
I need to create a bash shell script starting with a day and then
Starting with an empty dataframe, I need to fill the dataframe as follows: A
I'm starting a project where I need to use OpenJPA 2.2 and run on
Im just starting with localization today and need some information. I have a project
I need a regular expression able to match everything but a string starting with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.