I interpreted your post, at first, to mean the difference between the reference to map["hello"] method, and the vector building method which iMalc implemented, and had this to say about that:
No, and a small example could prove it:
Code:
#include <vector>
#include <string>
#include <map>
#include <iostream>
using namespace std;
void wysiwyg(map<string, vector<string> > &the_map)
{
for(map<string, vector<string> >::iterator i = the_map.begin(); i != the_map.end(); ++i) {
cout << i->first << " ->\n\t";
for(vector<string>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
cout << *j << ", ";
}
cout << '\n';
}
}
void foo(map<string, vector<string> > &the_map)
{
vector<string> bunch_o_strings;
bunch_o_strings.push_back("one");
bunch_o_strings.push_back("two");
bunch_o_strings.push_back("three");
bunch_o_strings.push_back("world");
the_map["hello"].swap(bunch_o_strings);
wysiwyg(the_map);
}
void bar(map<string, vector<string> > &the_map)
{
vector<string> &bunch_o_strings = the_map["hello"];
bunch_o_strings.push_back("one");
bunch_o_strings.push_back("two");
bunch_o_strings.push_back("three");
bunch_o_strings.push_back("world");
wysiwyg(the_map);
}
int main()
{
map<string, vector<string> > a_map_s_v_s;
foo(a_map_s_v_s);
bar(a_map_s_v_s);
foo(a_map_s_v_s);
}
#if 0
My output:
hello ->
one, two, three, world,
hello ->
one, two, three, world, one, two, three, world,
hello ->
one, two, three, world,
#endif
If you were right then we would have " one, two, three, world, one, two, three, world, one, two, three, world," at the end, but we swap in the new vector that map["hello"] points to in the foo function. The swap behavior is either right or wrong like iMalc said.