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

Discussion in 'C++' started by navS, May 9, 2008.

  1. navS

    navS Guest

    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
     
    navS, May 9, 2008
    #1
    1. Advertising

  2. navS

    Ian Collins Guest

    navS wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, May 9, 2008
    #2
    1. Advertising

  3. navS

    MiB Guest

    On 9 Mai, 11:22, navS <> 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
     
    MiB, May 9, 2008
    #3
  4. navS

    Ismo Salonen Guest

    navS wrote:
    > 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
     
    Ismo Salonen, May 9, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,080
  2. hazz
    Replies:
    6
    Views:
    49,952
    SkyUCHC
    Jun 9, 2010
  3. loveNUNO
    Replies:
    2
    Views:
    955
    loveNUNO
    Nov 20, 2003
  4. Robert Cohen
    Replies:
    3
    Views:
    296
    Andrew Durstewitz
    Jul 15, 2003
  5. rp
    Replies:
    1
    Views:
    563
    red floyd
    Nov 10, 2011
Loading...

Share This Page