473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Value output by a map

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

Jan 19 '07 #1
2 1579
"Peter Liu" <pp********************@yahoo.co.idwrote:
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 T2void 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>() );
Jan 19 '07 #2
"Peter Liu" <pp********************@yahoo.co.idwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
>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 T2void 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.
Jan 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
5
by: Member | last post by:
Hi All. Maybe someone in here could help on this too.... Uusally I can return a value from a stored procedure without any problem. Today I ran into something I cannot figure out. ...
4
by: Steven | last post by:
I'm calling a stored procedure which has an output parameter of type int. Once the stored procedure is executed, I want to check the value of the parameter in case it is null. However, when the a...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
8
by: Christopher Weaver | last post by:
I'm having trouble accessing the value of an output parameter of a stored procedure. The SP looks like this: SET TERM ^ ; CREATE PROCEDURE SP_NEW_TASK RETURNS ( "uidTask" INTEGER) AS begin
7
by: Rudy | last post by:
Hello All! I have a value in a textbox(txbTableIDm.Text ) that I would like to use in a paremiter in a SP I wrote, and then have the select statement work off that parememter, retireive a...
4
by: Tifer | last post by:
Hello, I'm still new to the whole .Net thing and I'm having a problem with something that should be so simple -- executing a query and returning an output parameter. It's a standard "Add"...
8
by: jbonifacejr | last post by:
Hi. I'm sorry to bother all of you, but I have spent two days looking at code samples all over the internet, and I can not get a single one of them to work for me. I am simply trying to get a value...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.