• Welcome to Valhalla Legends Archive.
 

uhm... noob question

Started by brew, March 21, 2007, 09:34 AM

Previous topic - Next topic

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?
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

MyndFyre

Have you looked into the STL string class?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

l2k-Shadow

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>
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

warz

#3
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);

MyndFyre

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...
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

K

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

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.