Value output by a map

P

Peter Liu

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
}
 
D

Daniel T.

Peter Liu said:
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>() );
 
J

Jim Langston

Peter Liu said:
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.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top