🔒 Locked
Simple string question...
Advertisement
Original Post
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
exactly!
rather than declaring it like you did, declare it like this:
then you have to allocate memory for it:
basically, it can now hold mylength-1 characters.
when you are done though, you have to free the memory:
hope that helps.
--- krez (krezisback@aol.com)
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)
eh, you can realloc() it (i rarely do this so you''ll have to look it up yourself)...
you could also use STL strings (which i know nothing about really, but everyone else is always talking about them).
--- krez (krezisback@aol.com)
you could also use STL strings (which i know nothing about really, but everyone else is always talking about them).
--- krez (krezisback@aol.com)
#include <iostream> |
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!
quote:
Original post by krez
sizeof(char)
sizeof is defined as the size in char''s, so...
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
Chris Barry
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
Chris Barry
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!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement