how to put the content of one hash map to another hash map

N

navS

Hi, team,
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..

Thanks in advance.

-naveen
 
I

Ian Collins

navS said:
Hi, team,
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..
u doesn't post here any more. What have you tried? Give us an attempt
and we'll give you some help.
 
M

MiB

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
 
I

Ismo Salonen

navS said:
Hi, team,
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..

Thanks in advance.

-naveen

Is this homework assignment ?
If those arrays are otherwise similar something like swap(map1,map2)
might work (if map2 is originally empty).

ismo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top