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?
Have you looked into the STL string class?
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>
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);
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...
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
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.