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 8055869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:31:54+00:00 2026-06-05T08:31:54+00:00

Is it possible with a STL algorithm to deep copy a std::map values to

  • 0

Is it possible with a STL algorithm to deep copy a std::map values to a std::set?

I don’t want to explicitly insert in the new set.

I don’t want to explicitly do this:

std::map<int, double*> myMap; //filled with something
std::set<double*> mySet;

for (std::map<int, double*>::iterator iter = myMap.begin(); iter!=myMap.end(); ++iter)
{
     mySet.insert(iter->second);
}

but find a more coincise and elegant way to do this, with a deep copy of values.

  • 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-06-05T08:31:56+00:00Added an answer on June 5, 2026 at 8:31 am

    What about this?

    std::transform(myMap.begin(), myMap.end(), std::inserter(mySet, mySet.begin()),
        [](const std::pair<int, double*>& key_value) {
            return key_value.second;
        });
    

    This only copies the pointers, though. If you want a deep-copy, then you would need to do:

    std::transform(myMap.begin(), myMap.end(), std::inserter(mySet, mySet.begin()),
        [](const std::pair<int, double*>& key_value) {
            return new double(*key_value.second);
        });
    

    BTW, the code uses lambda functions (only available from C++11). If you cannot use C++11, you could use a function object, though.

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

Sidebar

Related Questions

Possible Duplicate: In STL maps, is it better to use map::insert than []? I
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Is there a “Set” data structure in .Net? Duplicate: this is a
Ok, so I wrote an stl-like algorithm called cartesian_product . For those who don't
I would like to use algorithm std::include to work with heterogeneous STL collections. In
Possible Duplicates: Checking value exist in a std::map - C++ How to traverse a
Is there any way to return an STL iterator to a std::map (e.g. std::map<const
Possible Duplicate: Why isn't the [] operator const for STL maps? I've boiled this
Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use

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.