Archived

This topic is now archived and is closed to further replies.

Moe

Simple string question...

Recommended Posts

I have a fairly simple question about strings. I currently have something like char stringtofile[256] = "this is obviously less than 256 characters"; I want change that so I can have more than 256 characters, without using an array. Is it done with a pointer somehow, like: LPSTR stringtofile = "this can be longer than 256 characters"; I need to use it as a parameter in a function. Moe''s site

Share this post


Link to post
Share on other sites
exactly!

rather than declaring it like you did, declare it like this:
char* stringtofile; 

then you have to allocate memory for it:

/* c style */
stringtofile = malloc(mylength* sizeof(char));
// c++ style
stringtofile = new char[mylength];

basically, it can now hold mylength-1 characters.
when you are done though, you have to free the memory:

/* c style */
free stringtofile;
// c++ style
delete[] stringtofile;


hope that helps.

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
  #include <iostream>
#include <string> // string class header; no .h extension


using namespace std;
int main(int argc, char *argv[])
{
string str = "This is a string.";
cout << str;
str += " I appended a second string to it dynamically.";
cout << ''\n'' << str;
str += "\nAnd I appended a new line."
cout << str << endl;
}

Output:
This is a string.
This is a string. I appended a second string to it dynamically.
This is a string. I appended a second string to it dynamically.
And I appended a new line.


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
That string class thingy is part of the STL (Standard Template Library). If you know what Templates are, you''d know how they work.

Anyway, it''s real easy to use. Only problem is that win32 uses a lot of LPSTRs.

Share this post


Link to post
Share on other sites
Technically, class string isn''t part of the STL, not being a template class and all But it does share much of the functionality, especially in the modifying functions and iterators. Nice thing about class string is most operators and functions are overloaded six way ''til Sunday to work with char-strings, single char''s and other string objects (10 versions of insert() alone). A must-learn.

Chris Barry

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
actually it is a templated class, string is just a typedef for basic_string<char> or something like that.

Share this post


Link to post
Share on other sites
You sure? I''m pretty sure class string'' methods are optimized for dealing with the entire string as one object, while vectors are optimized for managing each container individually. Hmmm. Well, I guess the standard doesn''t say they can''t make string a typedef of an STL container and add new funtionality .....

Chris Barry

Share this post


Link to post
Share on other sites
quote:
Original post by 23yrold3yrold
I''m pretty sure class string'' methods are optimized for dealing with the entire string as one object, while vectors are optimized for managing each container individually.

?

*doesn''t get it*

Why are you using vector and string as comparable arguments? std::vector has nothing to do with std::string (which is a typedef for std::basic_string<char> btw); vector and string are are both containers and control sequences of data. That means there is little probability of similarity between them, because if there was one would simply be an adapter for the other.

Furthermore, std::string does not optimize anything - at least not in the popular Dinkumware implementation (which comes with MSVC). And according to this page, neither does the SGI implementation:
quote:
basic_string<charT, traits, Alloc>:
The types string and wstring are typedefs for, respectively, basic_string<char> and basic_string<wchar_t>.


The SGI STL site is a good resource. You can find the link in my .sig

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites