Reputation: 4536
Just a question on how copy is working...I need some verification for mental sanity purposes. If I have a:
multiset<pair<double, string> > myMultiset;
and I'm tring to print the contents out to console...I should expect an error when using:
copy(myMultiset.begin(), myMultiset.end(), ostream_iterator</.../>(cout, " "));
This is because for A. for the ostream_iterator I'm tring to pass type "pair" which ostream won't like? If the multiset was made of "pair", even though I'd only be passing type string through to "ostream" I feel like trying to pass two elements at once is going to cause an error. is there an iterator that will iterate over JUST the keys or just the values? Is there a clean way to handle this? Each time I end up just writing "for()" loops so that I can get at contents with "iter->first" and "iter->second"... I'm new to the STL Algo's but I love the idea of cleanliness and I'd like to leverage off their capabilities...So many minutae though...
Upvotes: 1
Views: 250
Reputation: 58675
The reason your std::copy()
won't work as is is because there is no std::operator<<()
defined for std::pair
. There are many reasonable ways a user might textually format a pair, and the standard left it undefined. You can either define one on your own:
template<typename K, typename V>
std::ostream& operator<<(std::ostream& out, std::pair<K,V> const& p)
{
return out << p.first;
}
// ...
std::copy(set.begin(), set.end(),
std::ostream_iterator<double>(std::cout, " "));
or define a function and use std::transform()
instead:
std::transform(set.begin(), set.end(),
std::ostream_iterator<double>(std::cout, " "),
[](std::pair<double,std::string> const& p) {
return p.first;
});
You might find the key-extracting lambda useful, and flesh a utility functor out of it for reuse:
struct keys_of {
template<typename K, typename V>
K operator()(std::pair<K,V> const& p) const
{
return p.first;
}
};
// ...
std::transform(set.begin(), set.end(),
std::ostream_iterator<double>(std::cout, " "),
keys_of());
Sometimes a for
loop does make things simpler, more concise, more immediately understandable. The new range-based for
loop takes this even further:
for( auto p : set ) {
std::cout << p.first << ' ';
}
This will behave slightly different than the other approaches in that it will leave a dangling space delimiter at the end. It also only works with a container; if you have a pair of iterators rather than a container (say, a pair of std::istream_iterator
or a pair of arbitrary iterators into a container) then you should use an algorithm. But I think that as it is it's the simplest and most clear of the bunch.
Upvotes: 4
Reputation: 18964
Are you using C++11? If so you could use thetransform
algorithm with a lambda to extract the key (or value).
You can do similar things in older C++, but it's a bit messier.
Upvotes: 1