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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:23:17+00:00 2026-06-09T03:23:17+00:00

Using STL in C++, how would I go about applying a function to each

  • 0

Using STL in C++, how would I go about applying a function to each value in a std::map to get a std::string (printed representation of the value) and collect the std::string(s) into a collection that is sorted by a floating point key which comes from another function applied to each corresponding value in the map?

Stated another way, I want to iterate over the key value pairs in the map and create a new set of key value pairs where the new key and value are a function of the old value.

double getNewKey(origValue value);
std::string getNewValue(origValue value);
// Or is it better to map both at once in a pair?
std::pair<double, std::string> getNewPair(origValue value);

std::map<origKey, origValue> origMap;

// Perform some transformation on each value of origMap to get a new map:
std::map<double, std::string> transformedMap =
  /* What goes here to use getNewKey() and getNewValue() or use getNewPair()? */
  ;

But, please without using C++11.

  • 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-09T03:23:17+00:00Added an answer on June 9, 2026 at 3:23 am

    std::transform is what you need:

    #include <map>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    // using a few C++11 features to make life easier
    int main(){
      std::map<int, int> src, dst; // example KV pair
      for(unsigned i=0; i < 10; ++i)
        src[i] = i;
      typedef std::map<int, int>::value_type kv_pair;
      std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
          [](kv_pair const& p){
            return kv_pair(p.first, p.second * 2);
          });
      for(auto& p : dst)
        std::cout << p.first << " : " << p.second << "\n";
    }
    

    Live example.

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

Sidebar

Related Questions

Using the STL C++ hash_map... class MyKeyObject { std::string str1; std::string str2; bool operator==(...)
I'm new to STL. The thing stumping me about using a map to store
I'm a bit confused about using STL set::find() for a set of my own
Previously, I am using STL map to perform the mentioned task. struct ltstr {
I'm using the STL map data structure, and at the moment my code first
The following is a C++ program using STL vector container. Just wanted to know
I am not using boost libraries. How can i do this using STL? class
I want to create in C++ an array of Objects without using STL. How
When using C++ STL containers, under what conditions must reference values be accessed? For
I want to print numbers to a file using the stl with the number

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.