Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: brew on March 21, 2007, 09:34 AM

Title: uhm... noob question
Post by: brew on March 21, 2007, 09:34 AM
sorry for asking but what is the most efficient way to create/store/print a string in C++? Is making my own "StringBuilder" class like in VB .NET a good idea?
Title: Re: uhm... noob question
Post by: MyndFyre on March 21, 2007, 10:55 AM
Have you looked into the STL string class?
Title: Re: uhm... noob question
Post by: l2k-Shadow on March 21, 2007, 02:23 PM
Quote from: brew on March 21, 2007, 09:34 AM
sorry for asking but what is the most efficient way to create/store/print a string in C++? Is making my own "StringBuilder" class like in VB .NET a good idea?

well the easiest way:
#include <string>
Title: Re: uhm... noob question
Post by: warz on March 21, 2007, 03:17 PM
include proper header files:

#include <stdio.h>
#include <string.h>


allocate space for a character array:

char myarray[5];


place a null terminated character array into the space, thus creating a string:

strcpy(myarray, "hey!");


print our string:

printf("the string: %s\n", myarray);
Title: Re: uhm... noob question
Post by: MyndFyre on March 21, 2007, 06:25 PM
I'm pretty sure that reallocing every time he wants to append to a string is the most efficient method of doing this, warz.  I'm also pretty sure that you're not using any kind of C++ techniques with that code...
Title: Re: uhm... noob question
Post by: K on March 21, 2007, 08:02 PM
Quote from: MyndFyre[vL] on March 21, 2007, 06:25 PM
I'm pretty sure that reallocing every time he wants to append to a string is the most efficient method of doing this, warz.

The std::string class will create a new instance every time it's modified as well.  If you want something mutable like a .NET StringBuilder, you'll want to use the std::stringstream found in the <sstream> header
Title: Re: uhm... noob question
Post by: warz on March 22, 2007, 03:16 PM
well, the fact of the matter is that the task at hand is nothing trivial. sure, use the stl string class if you want to C++'ify this, but it's not mandatory. the stl string class is nice, though, if you plan to use a bunch of stl classes like priority queue's and stuff. i used as many stl classes as i could in one of my older moderation clients - makes things simple.