Re: how to put the content of one hash map to another hash map
On 9 Mai, 11:22, navS <naveenkumarnl1...@gmail.com> wrote:
Can u please help me to transfer the contents of one hash map to
another and clear the first hash map.
And display the content of the second hash map
please give me the code..
You need to be more specific. What hash map are you talking about?
Open address hashing? Perfect hashing? Extensible hashing? Are the
source and target hash map the same implementation?
Traversing a hash map varies greatly depending on implementation. In
some you can iterate from one element to another, in others you
cannot. The hash function may depend on parameters of the hash map
instance (e.g. table size for open addressing), plus the order of the
traversal may result in a element sequence different from the insert
order.
I can give you some pseudo code:
HashX<KeyT, ValueT> source;
HashY<KeyT, ValueT> target;
for each element (key, value) in source { // traverse all elements
target.insert( key, value );
}
source.clear();
for each element (key, value) in target { // traverse all elements
std::cout << "Key = " << key << "; Value = " << value << std::endl;
}
If your hash maps support STL container like interface with iterators,
this may work:
target.clear();
target.insert( target.begin(), source.begin(), source.end() );
source.clear();
for ( HashY<KeyT,ValueT>::const_iterator p = target.begin(); p !=
target.end(); ++p ) {
// assuming, hash elements are stored as std::pair<KeyT,ValueT>.
std::cout << "Key = " << p->first << " Value = " << p->second <<
std::endl;
}
best,
Michael