Accessing the second in a pair via for_each?

M

Michael

This may seem like a sophomoric question, but I can't really seem to
find any documentation on this problem specifically.

I've created a map of key/value pairs, along with a static function
that accepts a value from the map (said function has no knowledge of
the map).

I'm attempting to iterate over the map, calling this function for
every value in the container. The problem is that for_each attempts
to pass an std::pair object rather than the std::pair.second that I'm
looking for. Note that this function was not created specifically for
the algorithm.

Are there any standard adapters to do this transition directly? If
not, how would you suggest doing this, short of writing a functor to
work with the pair directly.


- Michael
 
A

Alf P. Steinbach

* Michael:
This may seem like a sophomoric question, but I can't really seem to
find any documentation on this problem specifically.

I've created a map of key/value pairs, along with a static function
that accepts a value from the map (said function has no knowledge of
the map).

I'm attempting to iterate over the map, calling this function for
every value in the container. The problem is that for_each attempts
to pass an std::pair object rather than the std::pair.second that I'm
looking for. Note that this function was not created specifically for
the algorithm.

Are there any standard adapters to do this transition directly? If
not, how would you suggest doing this, short of writing a functor to
work with the pair directly.

What's wrong with writing a functor?

For that matter, and more importantly, what's wrong with an ordinary
for-loop?

Why are you using for_each?
 
B

Barry

Michael said:
This may seem like a sophomoric question, but I can't really seem to
find any documentation on this problem specifically.

I've created a map of key/value pairs, along with a static function
that accepts a value from the map (said function has no knowledge of
the map).

I'm attempting to iterate over the map, calling this function for
every value in the container. The problem is that for_each attempts
to pass an std::pair object rather than the std::pair.second that I'm
looking for. Note that this function was not created specifically for
the algorithm.

Are there any standard adapters to do this transition directly? If
not, how would you suggest doing this, short of writing a functor to
work with the pair directly.


- Michael

sgi STL provides select1st, and select2nd as extension

check select2nd out
 
J

James Kanze

This may seem like a sophomoric question, but I can't really seem to
find any documentation on this problem specifically.
I've created a map of key/value pairs, along with a static function
that accepts a value from the map (said function has no knowledge of
the map).
I'm attempting to iterate over the map, calling this function for
every value in the container. The problem is that for_each attempts
to pass an std::pair object rather than the std::pair.second that I'm
looking for. Note that this function was not created specifically for
the algorithm.
Are there any standard adapters to do this transition directly? If
not, how would you suggest doing this, short of writing a functor to
work with the pair directly.

This should be possible with boost::bind, I think. (I'm pretty
sure that Boost::bind will be in the next version of the
standard. Without the "boost" in the name, of course.)

You might also want to check out boost::iterator for this sort
of thing. An iterator which returns a reference to the value or
the key, rather than to the pair, seems like the most natural
way of handling this to me.
 
P

Pete Becker

This should be possible with boost::bind, I think. (I'm pretty
sure that Boost::bind will be in the next version of the
standard. Without the "boost" in the name, of course.)

It's currently in the Working Draft for the next revision of the
standard, under the name std::bind. It's also available in
implementations of TR1, under the name std::tr1::bind.
 
M

Michael

It's currently in the Working Draft for the next revision of the
standard, under the name std::bind. It's also available in
implementations of TR1, under the name std::tr1::bind.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


Thank you guys, that answered my question. Since TR1 isn't yet
required for standards conforming code I'll leave it in a normal
loop :)

Alf:
It currently is in a for loop. The reason I posted is because I saw a
need that I imagined was relatively common and took the opportunity to
learn.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top