473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Jul 22 '05 #1
3 2601

"lallous" <la*****@lgwm.org> wrote in message
news:bu************@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
Jul 22 '05 #2
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
Jul 22 '05 #3
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
8
by: Woodster | last post by:
I am starting to use templates and have managed to figure out how to use std::string, std::map and make_pair successfully so far (Yeah I know - not much of a big step but I'm getting there) ...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
2
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace...
1
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
4
by: Ramesh | last post by:
Hi, I need to convert unsigned long data to a std::string, I googled a bit to see if the string class supports any methods to achieve this - but didnt find any, I think of using sprintf and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.