On Fri, 2015-04-03, SpreadTooThin wrote:
> On Thursday, April 2, 2015 at 9:33:28 PM UTC-6, SpreadTooThin wrote:
>> This is my map.
>>
>> std::map<string, std::vector<string> > myMap;
>>
>> Given that key is a string and the element that it represents
>> is a vector of strings:
>>
>> How do I add an element to myMap?
>>
>> std::string key = "abc";
>> std::vector<string> * empty = new std::vector<string>;
>> map[key]=*empty;
>>
>> To add an element to the vector withing the map
>>
>> std::vector<string> *myvect;
>> myvect = map[key];
>> *myvect->push_back(newstring);
>>
>> Shouldn't I be using a refrence to an object rather than a pointer?
>
> std::vector<string> & key
> Isn't this a reference...
Yes.
> Doesn't it seem inefficient to be copying objects...
> Like I'd rather use a pointer to an object but then
> I'd have to be careful about freeing it...
All the standard library containers copy. They own the objects they
contain. That's the way it is. In the rare cases where this is
inefficient, you'd store pointers or smart pointers instead -- but I
don't think I ever had to, for that particular reason.
(And this design got even cheaper with C++11.)
Also see Ian Collins' posting.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/
snipabacken.se> O o .