Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C++ strings?  (Read 4263 times)

0 Members and 1 Guest are viewing this topic.
C++ strings?
« on: April 12, 2006, 05:35:37 pm »
  • Its the joeshmo!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 694
How can I declare a string in c++? I want to do something like

waste=new int[50000]

exept with a string, like

waste=new string[500000]

Any help? Thanks.
« Last Edit: February 28, 2007, 09:58:13 pm by 4Sword »
Logged
  • Netterra Virtual Pets

Ben

Re: C++ strings?
« Reply #1 on: April 12, 2006, 06:25:12 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
There are two ways, one is the C way of using char arrays, and the other is the C++ way of using a string class.
A char array is simply just the same as int[5000] but char[5000] and the 5000 is how many characteres that are in it.
Though that's really silly because it just uses a quarter of the memory an int does.
I really don't see what the point of your string is :-p

But if you want to use strings then go here:
http://www.cppreference.com/cppstring/index.html

Very useful if you can fathom it. Oh and btw c libraries like iostream use c strings (character arrays) and you can get one of these from a string class by using like
string.c_str(). That turns the contents of a string into a char array.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion
Re: C++ strings?
« Reply #2 on: April 12, 2006, 06:29:29 pm »
  • Its the joeshmo!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 694
Hmm. Thanks.
Logged
  • Netterra Virtual Pets
Re: C++ strings?
« Reply #3 on: April 12, 2006, 06:49:50 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
I tend to just use char pointers, its not the best way or the easiest to do modification on the string, but its simpler to declare and use.

you can declare char array like so;
Code: [Select]
char* MyString;
MyString = "Hello World";
Logged
Re: C++ strings?
« Reply #4 on: April 12, 2006, 06:51:15 pm »
  • Its the joeshmo!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 694
I tend to just use char pointers, its not the best way or the easiest to do modification on the string, but its simpler to declare and use.

you can declare char array like so;
Code: [Select]
char* MyString;
MyString = "Hello World";
whats the difference in doing this and just declaring it as
char mystring?
Logged
  • Netterra Virtual Pets
Re: C++ strings?
« Reply #5 on: April 12, 2006, 06:54:32 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
I tend to just use char pointers, its not the best way or the easiest to do modification on the string, but its simpler to declare and use.

you can declare char array like so;
Code: [Select]
char* MyString;
MyString = "Hello World";
whats the difference in doing this and just declaring it as
char mystring?

Put simply, char mystring would only be able to hold 1 character, char* mystring can hold as many as you want.
Logged
Re: C++ strings?
« Reply #6 on: April 12, 2006, 06:56:32 pm »
  • The Broken King
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1259
char mystring would just be a single character. However, an array is a pointer to the head of the array, and a char pointer works essentially the same way as a char array.

If you're using C++.net, there's a managed String class, but it's really tough to work with (I've used strings in probably a dozen languages and only that one has confused me :P), but it has a lot of functions too, so supposedly it's useful.
Logged
  • Broken Kings [Temp Site]

Max

Re: C++ strings?
« Reply #7 on: April 12, 2006, 07:00:25 pm »
  • Crazy Monkey
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 72
char mystring creates a single character variable. char *mystring creates a pointer to a character.  Since all arrays are are contigious memory spaces holding the same type of data (char array, int array), and the first entry in the array is denoted by the memory address of the variable name, you create a string(an array of characters ending in a '\0') via creating a pointer to the first element in the string.  For instance: you create an array char a[512], using the variable a gives you the memory address of the first part of the array, you can call a[0], by using *a.  Similarly char *a will do the same thing, except you are not defining the maximum amount of chars the array can hold.  Each have their own uses for creating a string.
Logged
~The Gaurdians of ZFGC~
Kirby, metallica48423, Max, Vash, walnut100

aab

^ Evolved from a Hobbit
Re: C++ strings?
« Reply #8 on: April 13, 2006, 04:39:25 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
the std::string is quite easy to use, and compatible with things like std::stringstream (#include<sstream>) and the ios classes, making it nice and easy to use.
eg:
Code: [Select]
std::string myName = "aab",myDetails;
std::stringstream ss;

if( myName == "aab" )
{
ss<< "my name is: " << myName << ", and my age is: " << 18 ;
myDetails = ss.str();
std::cout<< myDetails << std::endl;
}


for  // loop through with an iterator: same syntax as many standard objects: efficient: iterator is really a pointer, so this is like having a char [] and looping from char [0] to the last one, but safer and faster, though you could just use a char* and use ++ on it the same way
(
std::string::iterator
i = myDetails.begin();
i != myDetails.end();
++i
)
{
*i = 'A';
}

std::cout<< myDetails << std::endl;
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace
Re: C++ strings?
« Reply #9 on: April 13, 2006, 04:49:00 pm »
  • Its the joeshmo!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 694
String functions are included in Iostream?
Logged
  • Netterra Virtual Pets

aab

^ Evolved from a Hobbit
Re: C++ strings?
« Reply #10 on: April 13, 2006, 06:15:38 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
cstring for string
sstream for stringstream
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.017 seconds with 55 queries.