Valhalla Legends Forums Archive | C/C++ Programming | uhm... noob question

AuthorMessageTime
BreW
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?
March 21, 2007, 2:34 PM
Myndfyr
Have you looked into the STL string class?
March 21, 2007, 3:55 PM
l2k-Shadow
[quote author=brew link=topic=16519.msg166972#msg166972 date=1174487685]
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?
[/quote]

well the easiest way:
#include <string>
March 21, 2007, 7:23 PM
warz
include proper header files:
[code]
#include <stdio.h>
#include <string.h>
[/code]

allocate space for a character array:
[code]
char myarray[5];
[/code]

place a null terminated character array into the space, thus creating a string:
[code]
strcpy(myarray, "hey!");
[/code]

print our string:
[code]
printf("the string: %s\n", myarray);
[/code]
March 21, 2007, 8:17 PM
Myndfyr
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...
March 21, 2007, 11:25 PM
K
[quote author=MyndFyre[vL] link=topic=16519.msg166994#msg166994 date=1174519508]
I'm pretty sure that reallocing every time he wants to append to a string is the most efficient method of doing this, warz.
[/quote]

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
March 22, 2007, 1:02 AM
warz
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.
March 22, 2007, 8:16 PM

Search