[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

map inserts by reference or via copy ctor

puzzlecracker

9/25/2008 4:58:00 PM

Does map copy the object, both key and value during the insert?


is this example correct:

std::map<std::string T> my_map;

template<typename T>
void foo(const std::string str, const T & t){
my_map.insert(make_pair(str, t));
}

template<typename T>
void bar()
{
foo(std::string("Key1"),T(param1, param2));
}
39 Answers

Victor Bazarov

9/25/2008 5:17:00 PM

0

puzzlecracker wrote:
> Does map copy the object, both key and value during the insert?

Yes.

> is this example correct:
>
> std::map<std::string T> my_map;

I think you meant

std::map<std::string, T> my_map;

>
> template<typename T>
> void foo(const std::string str, const T & t){

Passing the string by value? You ought to do

void foo(const std::string & str, const T & t){

(although it doesn't make it incorrect, per se).

> my_map.insert(make_pair(str, t));

'make_pair' will either create a wrong pair (the second template
argument would be a reference to const, or it will be the right pair,
but then creation of it will make the first copy. The insertion itself
will [also] make a copy since the temporary created by 'make_pair' will
be destroyed at the end of the full expression.

> }
>
> template<typename T>
> void bar()
> {
> foo(std::string("Key1"),T(param1, param2));

Unless you have some other overload of 'foo', there is no need to spell
out the type of the first argument, there exists an implicit conversion
from a character pointer to 'std::string'. You could just write

foo("Key1", T(param1, param2));

> }

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Obnoxious User

9/25/2008 5:27:00 PM

0

On Thu, 25 Sep 2008 09:57:41 -0700, puzzlecracker wrote:

> Does map copy the object, both key and value during the insert?
>
>
> is this example correct:
>
> std::map<std::string T> my_map;
>
> template<typename T>
> void foo(const std::string str, const T & t){
> my_map.insert(make_pair(str, t));
> }
>
> template<typename T>
> void bar()
> {
> foo(std::string("Key1"),T(param1, param2));
> }

You could try answering the question yourself with
the following program.

#include <iostream>
#include <map>

class C {
private:
int d_value;
public:
C(int v) : d_value(v) {
std::cout<<this<<"\tC::C(";
std::cout<<d_value<<")"<<std::endl;
}
C(C const & c) : d_value(c.d_value) {
std::cout<<this<<"\tC::C(C const & ";
std::cout<<d_value<<" )"<<std::endl;
}
~C() {
std::cout<<this<<"\tC::~C(";
std::cout<<d_value<<")"<<std::endl;
}
int value() const {
return d_value;
}
};

bool operator<(C const & c1, C const & c2) {
return c1.value() < c2.value();
}

int main() {
std::map<C,C> m;
m.insert(std::make_pair(C(4),C(-4)));
m.insert(std::make_pair(C(7),C(-7)));
m.insert(std::make_pair(C(1),C(-1)));
m.insert(std::make_pair(C(2),C(-2)));
return 0;
}

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

linuxgal

2/16/2014 4:50:00 AM

0

Truth and honesty wrote:
> So now you are saying cold weather proves global warming.

And if we have a heat wave next summer, that proves global warming too.

thomas p.

2/16/2014 8:08:00 AM

0

"Truth and honesty" <Truth@honesty.com> skrev i meddelelsen
news:9lujcccqvy8q.7vps7zb8oobg$.dlg@40tude.net...
> On Sat, 15 Feb 2014 20:59:08 -0200, John Manning wrote:
>
>> On 2/15/2014 8:16 PM, hlillywh@juno.com wrote:
>>> On Saturday, February 15, 2014 11:45:57 AM UTC-8, spencer wrote:
>>>> On Saturday, February 15, 2014 12:37:49 PM UTC-6, John Manning wrote:
>>>> http://editorialcartoonists.com/cartoons/HorseD/2014/HorseD201402...
>>> Global warmists always have an excuse for why their predictions fail.
>>> And it's amazing how the statists seem to think that some cartoon, or
>>> some photo with no context actually proves something.
>>
>>
>> Hilly Whoo is either pretending he doesn't recognize the purpose of
>> editorial cartoons
>> and political placards - or - he's really, really clueless about the
>> world around him.
>
>
> So now you are saying cold weather proves global warming.


How long have you had these hallucinations?

--
thomas p

Ignorance is the mother of devotion.

David Hume


Truth and honesty

2/16/2014 8:48:00 AM

0

On Sat, 15 Feb 2014 20:49:44 -0800, linuxgal wrote:

> Truth and honesty wrote:
>> So now you are saying cold weather proves global warming.
>
> And if we have a heat wave next summer, that proves global warming too.

And if it rains a little he will be saying it or if it does not rain. It
just piles up.

Truth and honesty

2/16/2014 8:49:00 AM

0

On Sun, 16 Feb 2014 09:07:40 +0100, thomas p. wrote:

> "Truth and honesty" <Truth@honesty.com> skrev i meddelelsen
> news:9lujcccqvy8q.7vps7zb8oobg$.dlg@40tude.net...
>> On Sat, 15 Feb 2014 20:59:08 -0200, John Manning wrote:
>>
>>> On 2/15/2014 8:16 PM, hlillywh@juno.com wrote:
>>>> On Saturday, February 15, 2014 11:45:57 AM UTC-8, spencer wrote:
>>>>> On Saturday, February 15, 2014 12:37:49 PM UTC-6, John Manning wrote:
>>>>> http://editorialcartoonists.com/cartoons/HorseD/2014/HorseD201402...
>>>> Global warmists always have an excuse for why their predictions fail.
>>>> And it's amazing how the statists seem to think that some cartoon, or
>>>> some photo with no context actually proves something.
>>>
>>>
>>> Hilly Whoo is either pretending he doesn't recognize the purpose of
>>> editorial cartoons
>>> and political placards - or - he's really, really clueless about the
>>> world around him.
>>
>>
>> So now you are saying cold weather proves global warming.
>
>
> How long have you had these hallucinations?

Ask John, I did not say it.

Free Lunch

2/16/2014 2:30:00 PM

0

On Sun, 16 Feb 2014 15:47:12 +1100, Truth and honesty
<Truth@honesty.com> wrote in alt.atheism:

>On Sat, 15 Feb 2014 20:59:08 -0200, John Manning wrote:
>
>> On 2/15/2014 8:16 PM, hlillywh@juno.com wrote:
>>> On Saturday, February 15, 2014 11:45:57 AM UTC-8, spencer wrote:
>>>> On Saturday, February 15, 2014 12:37:49 PM UTC-6, John Manning wrote:
>>>> http://editorialcartoonists.com/cartoons/HorseD/2014/HorseD201402...
>>> Global warmists always have an excuse for why their predictions fail.
>>> And it's amazing how the statists seem to think that some cartoon, or some photo with no context actually proves something.
>>
>>
>> Hilly Whoo is either pretending he doesn't recognize the purpose of
>> editorial cartoons
>> and political placards - or - he's really, really clueless about the
>> world around him.
>
>
>So now you are saying cold weather proves global warming.

Look at the climate for the entire world and tell us what is going on.

Free Lunch

2/16/2014 2:30:00 PM

0

On Sun, 16 Feb 2014 19:48:21 +1100, Truth and honesty
<Truth@honesty.com> wrote in alt.atheism:

>On Sat, 15 Feb 2014 20:49:44 -0800, linuxgal wrote:
>
>> Truth and honesty wrote:
>>> So now you are saying cold weather proves global warming.
>>
>> And if we have a heat wave next summer, that proves global warming too.
>
>And if it rains a little he will be saying it or if it does not rain. It
>just piles up.

Ask the British about 'raining a little'.

Mulligan

2/16/2014 2:44:00 PM

0

On Sunday, February 16, 2014 9:29:54 AM UTC-5, Free Lunch wrote:


> Look at the climate for the entire world and tell us what is going on.

I did and posted the real data.
The arctic ice has melted dramatically, much more so than normal, since the atomic weapons started being dropped on the earth.

thomas p.

2/16/2014 2:59:00 PM

0

"Truth and honesty" <Truth@honesty.com> skrev i meddelelsen
news:qievjs5khu55.pb44af5nques$.dlg@40tude.net...
> On Sun, 16 Feb 2014 09:07:40 +0100, thomas p. wrote:
>
>> "Truth and honesty" <Truth@honesty.com> skrev i meddelelsen
>> news:9lujcccqvy8q.7vps7zb8oobg$.dlg@40tude.net...
>>> On Sat, 15 Feb 2014 20:59:08 -0200, John Manning wrote:
>>>
>>>> On 2/15/2014 8:16 PM, hlillywh@juno.com wrote:
>>>>> On Saturday, February 15, 2014 11:45:57 AM UTC-8, spencer wrote:
>>>>>> On Saturday, February 15, 2014 12:37:49 PM UTC-6, John Manning wrote:
>>>>>> http://editorialcartoonists.com/cartoons/HorseD/2014/HorseD201402...
>>>>> Global warmists always have an excuse for why their predictions fail.
>>>>> And it's amazing how the statists seem to think that some cartoon, or
>>>>> some photo with no context actually proves something.
>>>>
>>>>
>>>> Hilly Whoo is either pretending he doesn't recognize the purpose of
>>>> editorial cartoons
>>>> and political placards - or - he's really, really clueless about the
>>>> world around him.
>>>
>>>
>>> So now you are saying cold weather proves global warming.
>>
>>
>> How long have you had these hallucinations?
>
> Ask John, I did not say it.


No, he didn't, making you either loony or a liar.

--
thomas p

Ignorance is the mother of devotion.

David Hume