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

The Archive Base Latest Questions

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

Do I need to allocate a pair if I insert it into a map

  • 0

Do I need to allocate a pair if I insert it into a map from a different scope?

#include <iostream>
#include <string>
#include <unordered_map>
#include <utility>

using namespace std;
void parseInput(int argc, char *argv[], unordered_map<string, string>* inputs);

int main(int argc, char *argv[])
{
    unordered_map<string, string>* inputs = new unordered_map<string, string>;
    parseInput(argc, argv, inputs);
    for(auto& it : *inputs){
        cout << "Key: " << it.first << " Value: " << it.second << endl;
    }
    return 0;
}

void parseInput(int argc, char *argv[], unordered_map<string, string>* inputs)
{
    int i;
    for(i=1; i<argc; i++){
        char *arg = argv[i];
        string param = string(arg);
        long pos = param.find_first_of("=");
        if(pos != string::npos){
            string key = param.substr(0, pos);
            string value = param.substr(pos+1, param.length()-pos);
            inputs->insert( make_pair(key, value) );//what happens when this goes out of scope
        }
    }
    for(auto& it : *inputs){
        cout << "Key: " << it.first << " Value: " << it.second << endl;
    }
}
  • 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-28T08:02:58+00:00Added an answer on May 28, 2026 at 8:02 am

    Its fine:

    inputs->insert( make_pair(key, value) );//what happens when this goes out of scope
    

    std::make_pair returns the result by value.

    The above has the same affect as:

    inputs->insert( std::pair<std::string, std::string>(key, value) );
    

    In both cases the value passed to insert() is copied(or moved) into the map.

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

Sidebar

Related Questions

For an assignment, I need to allocate a dynamic buffer, using malloc() for the
If I use std::vector<> or std::string, do I need to allocate them in heap
I'm working with C++ for a project. I need to store a pair (string,integer)
I need to allocate arrays of structures in a bunch of different places in
That's pretty much it. I need to allocate memory and pass it to a
I need to wrap a dynamically allocated array(from a = new double[100] for example)
Greetings, Would like to know if we need to explicitly free the string allocated
suppose I need to allocate and delete object on heap frequently (of arbitrary size),
what are my options when i need to allocate virtual memory beyond the limits
In C/C++ under Linux, I need to allocate a large (several gigabyte) block of

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.