If cstr is a pointer to a null-terminated C string, you can construct
an std::string from it.
For example:
return str::string( cstr );
One caveat: cstr must not be a NULL pointer (or behavior will be undefined).
I hope this helps,
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Soft Dev Manger, xitact <> http://www.xitact.com
Brainbench MVP for C++ <> http://www.brainbench.com
This worked for me:
#include<iostream>
#include<string>
int main() {
char phrase[] = "Hello ";
std::string s (phrase);
std::cout << s << std::endl;
return 0;
}
This is better.
std::string s (phrase, sizeof(phrase) -1);
For the purpose of this discussion, you're 100% correct, I'm just
pointing out a source of inefficiency I see all too often.
Have you timed it? How much less efficient is it really? Does this kind of
thing really make a critical difference in the speed of your code?
Assign it.
std::string str;
str = "this is a string";
-Kevin
Why assign it? why not use the constructor?
std::string str( "this is a string" );
NeilB
Nikhil
"Neil Butterworth" <neil_but...@lineone.net> wrote in message news:<3e5fba61$1...@mk-nntp-1.news.uk.worldonline.com>...
Please don't top post - rearranged:
> Quite honestly, I'm not sure if it makes a difference in this case,
If you are not sure, then why share your uncertainty with us? I am sure taht
it makes a difference.
> but I know that there certainly are instances when the assignment
> operator and the constructor behave differently...
They behave differently in _all_ circumstances.
NeilB
...which will fail as soon as someone makes this kind of change:
int main()
{
// ...
// search argv for index of phrase
// ...
char* phrase = argv[index];
std::string s( phrase, sizeof(phrase) - 1 );
// ...
}
Either comment the heck out of that efficiency decision, or use the one that
works in either case. Even though you understand it, you might not notice
it the next time you work on that code, or some C++ newbie will be assigned
to maintain your code and not notice it.
8 seconds vs 23 seconds .... you guess which one takes longer !
#include <string>
char foo[16384]; // if this is 16 bytes there is still a 30% delta
std::string maker1()
{
return std::string( foo, sizeof(foo) -1 );
}
std::string maker2()
{
return std::string( foo );
}
int main()
{
for ( int i = 0; i < sizeof( foo ) -1; i ++ )
{
foo[i] = 'A';
}
for ( int j = 0; j < 1200000; j ++ )
{
maker2();
}
return 0;
}
> #include <string>
>
> char foo[16384]; // if this is 16 bytes there is still a 30% delta
>
> std::string maker1()
> {
> return std::string( foo, sizeof(foo) -1 );
> }
>
> std::string maker2()
> {
> return std::string( foo );
> }
>
> int main()
> {
> for ( int i = 0; i < sizeof( foo ) -1; i ++ )
> {
> foo[i] = 'A';
> }
You don't zero terminate foo.
Regards.
Use the range constructor if the array is not null terminated.
std::string(cstr, cstr+size_of_cstr);
Fraser.
Yes I do ! This behaviour is well defined.
Regards
G
He was talking about cstr == NULL, not the string containing it
having a null.