question about std::string/map



 DEVELOP > c-Plus-Plus > question about std::string/map

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "lallous"
Date: 23 Jan 2004 06:51:31 AM
Object: question about std::string/map
Hello,
I have:
std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;
is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?
(b) m[p] = p1;
Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?
Btw, can someone tell me about std::string and its reference count issues?
--
Elias
.

User: "Sharad Kala"

Title: Re: question about std::string/map 23 Jan 2004 07:14:34 AM
"lallous" <lallous@lgwm.org> wrote in message
news:bur5od$lansr$1@ID-161723.news.uni-berlin.de...

Hello,

I have:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

Yes


(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?

Yes

Or should I use: map[s1] = "value"; ?

No

Btw, can someone tell me about std::string and its reference count issues?

That's implementation dependent.
Say p1 and p2 are string objects and both contain the string "Sharad".
Then an implementation could use reference counting till no modifying operation
occurs on either of the strings.
Read Scott Meyers MEC++ which talks about such cases of reference counting.
Best wishes,
Sharad
.

User: "Kamil Burzynski"

Title: Re: question about std::string/map 23 Jan 2004 07:11:36 AM
On Fri, 23 Jan 2004 14:51:31 +0200, lallous wrote:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";
(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

s1 will do its own copy

(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?

In m[p]=p1, at first string will be created with copy of p, then m[] will
get is as argument, second string will be created with copy of p1 and
then assigned to m[p]
This is equivalent:
string sp( p );
string sp1( p1 );
m[ sp ] = sp1;

Btw, can someone tell me about std::string and its reference count issues?

AFAIK std::string does not have to do reference counting (but it is
allowed to). If it does share string between its instances, it is
copy-on-write algorithm.
--
Best regards from
Kamil Burzynski
.

User: "Kevin Goodsell"

Title: Re: question about std::string/map 23 Jan 2004 01:52:19 PM
lallous wrote:

Hello,

I have:

std::map<std::string, std::string> m;
std::string s1;
char *p = "hello world", *p1 = "value";

Don't do this. It makes use of a dangerous and deprecated language
feature - the implicit conversion of a string literal to a (non-const)
char pointer. Always use 'const char *' or 'const char *const' for
pointing to a string literal. This way the compiler will warn you if you
attempt to modify the string literal. Without it, your code will
silently invoke undefined behavior if you attempt to modify a string
literal.

(a) s1 = p;

is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
string now?

(b) m[p] = p1;

Is that okay? will that create a valid entry into the map?
Or should I use: map[s1] = "value"; ?

Btw, can someone tell me about std::string and its reference count issues?

I don't believe there are any such issues. Whether or not std::string
uses reference counting is an implementation issue, and transparent to
the programmer.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER