Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:57:04+00:00 2026-05-28T15:57:04+00:00

Just a question on how copy is working…I need some verification for mental sanity

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:57:05+00:00Added an answer on May 28, 2026 at 3:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a question about copy/paste workflow in gVim. Right now I'm working on a
just a question that needs a quick answer, I have a Action, lets say,
just a quick question: I am a CS undergrad and have only had experience
just a quick question, if I have a matrix has n rows and m
Hallo all, Just now I tried to upload a modified working copy to its
Often while working on a branch I need to introduce some temporary changes (such
I have a quite complicated question to ask :) I am currently working on
I've just read this question , and I have a question about it: How
This is just a question to satisfy my curiosity. But to me it is
This is just a question to help me understand CSS rendering better. Lets say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.