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…
The reason your
std::copy()won’t work as is is because there is nostd::operator<<()defined forstd::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:or define a function and use
std::transform()instead:You might find the key-extracting lambda useful, and flesh a utility functor out of it for reuse:
Sometimes a
forloop does make things simpler, more concise, more immediately understandable. The new range-basedforloop takes this even further: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_iteratoror 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.