Value output by a map

Discussion in 'C++' started by Peter Liu, Jan 19, 2007.

  1. Peter Liu

    Peter Liu Guest

    I have many structs, each of which has only one member, for example,
    hehehh

    I wrote a template for all of them, but I still want my function foo
    --standalone-- to output the member of the struct used as a value of a
    the declared map.
    I guess this's also possible but don't know how .

    Thanks

    A simple example, T2 will be any struct.

    struct A{int a};
    struct B{int b};

    template<class T1, class T2> void outpyt(std::map<T1, T2>&m)
    {
    typename std::map<T1, T2>::const_iterator it=m.begin();
    for(;it!=m.end();++it)
    //Output the second which is the member of the struct T2
    }
     
    Peter Liu, Jan 19, 2007
    #1
    1. Advertising

  2. Peter Liu

    Daniel T. Guest

    "Peter Liu" <> wrote:

    > I have many structs, each of which has only one member, for example,
    > hehehh
    >
    > I wrote a template for all of them, but I still want my function foo
    > --standalone-- to output the member of the struct used as a value of a
    > the declared map.
    > I guess this's also possible but don't know how .
    >
    > Thanks
    >
    > A simple example, T2 will be any struct.
    >
    > struct A{int a};
    > struct B{int b};
    >
    > template<class T1, class T2> void outpyt(std::map<T1, T2>&m)
    > {
    > typename std::map<T1, T2>::const_iterator it=m.begin();
    > for(;it!=m.end();++it)
    > //Output the second which is the member of the struct T2
    > }


    Write an operator<< for each type that is the value of a map. Then:

    template < typename It >
    void output( It first, It last ) {
    while ( first != last ) {
    cout << first->second;
    ++first;
    }
    }

    Of course, we don't want the function to be limited to only cout do we?
    So:

    template < typename It >
    void output( It first, It last, ostream& os ) {
    while ( first != last ) {
    os << first->second;
    ++first;
    }
    }

    What if we want to copy all the values into another container? Then:

    template < typename It1, typename it2 >
    void output( It1 first, It1 last, It2 result ) {
    while ( first != last ) {
    result = first->second;
    ++first;
    ++result;
    }
    }

    We can use an ostream_iterator on the above when we want to send the
    output to an ostream instead of another container.

    Now, the only difference between that last output function and the
    standard copy function is that we do something special to the element
    before assigning it. std::transform is for that sort of thing:

    template < typename Pair >
    struct select2nd :
    std::unary_function< Pair, typename Pair::second_type >
    {
    typename Pair::second_type& operator()( Pair& x ) const {
    return x.second;
    }
    };

    template < typename It1, typename It2 >
    void output( It1 first, It1 last, It2 result ) {
    transform( first, last, result,
    select2nd< typename iterator_traits< It1 >::value_type>() );
    }

    So you could do this instead of using your output function:

    typedef map< int, int > MyMap;
    MyMap myMap;

    transform( myMap.begin(), myMap.end(),
    ostream_iterator<MyMap::mapped_type>( cout ),
    select2nd<MyMap::value_type>() );
     
    Daniel T., Jan 19, 2007
    #2
    1. Advertising

  3. Peter Liu

    Jim Langston Guest

    "Peter Liu" <> wrote in message
    news:...
    >I have many structs, each of which has only one member, for example,
    > hehehh
    >
    > I wrote a template for all of them, but I still want my function foo
    > --standalone-- to output the member of the struct used as a value of a
    > the declared map.
    > I guess this's also possible but don't know how .
    >
    > Thanks
    >
    > A simple example, T2 will be any struct.
    >
    > struct A{int a};
    > struct B{int b};
    >
    > template<class T1, class T2> void outpyt(std::map<T1, T2>&m)
    > {
    > typename std::map<T1, T2>::const_iterator it=m.begin();
    > for(;it!=m.end();++it)
    > //Output the second which is the member of the struct T2
    > }


    You have a few options depend on how you will using it.

    The problem comes in with a structure such as:

    struct C{ int a; float b; bool c; };

    What would you want the output to be?

    The possibly best solution is to either write an operator<< or operator int
    if you wanted just an int value, but you say these are POD.

    Tell exactly what you would want output for a more complex POD, more than
    one int var.
     
    Jim Langston, Jan 22, 2007
    #3
    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. alex
    Replies:
    1
    Views:
    659
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    8,011
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    373
  4. PGK
    Replies:
    1
    Views:
    532
  5. Rui Maciel
    Replies:
    2
    Views:
    3,117
    AndrewDover
    Dec 1, 2009
Loading...

Share This Page