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;
}
}
Its fine:
std::make_pair returns the result by value.
The above has the same affect as:
In both cases the value passed to insert() is copied(or moved) into the map.