Sorry to be clogging up the forums, but I have another question. As I make my way through the tutorial, I came across the data types char and string. I know that how char works and its array-like properties, but don't understand the difference between it and string. If there is a difference, could someone explain to me what makes string and char different, and possibly how, when, and why to use string and char in programs.
Thanks,
-Ameobea
If you can use strings with std, why have the include statement #include <string> in the same file? I understand a program will compile fine if std::string is present and the #include <string> statement is not present. Does the std library have the string class in it?
As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters. Can you directly read values into a string and a char using cin, or is there some special way of doing this?
As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters. Can you directly read values into a string and a char using cin, or is there some special way of doing this?
char is not a specialized way of manipulating the characters of a string, however there is a relationship in the sense that strings are essentially arrays of chars, so you can pluck out elements of a string and treat them as chars. You can use cin for both char and strings, however typically you will read strings with the getline function.
I understand a program will compile fine if std::string is present and the #include <string> statement is not present. Does the std library have the string class in it?
That's not correct. You do need to #include <string> to use a std::string. It might be that another standard header you're using already includes <string>, but you shouldn't count on that. Incidentally, the standard library is spread across the many standard headers (such as <string>).
Ameobea wrote:
As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters.
Not quite. A std::string is simply a container of chars. A char is a basic built-in type, just like int, double and so on. In C, there's no std::string, so a raw array of chars is used, with a null character marking where it ends, since arrays don't "know" their own size. Sometimes, when using a C library, you'll have to convert a std::string into a C string. C++ code shouldn't use C strings unless there's a good reason for that.
String objects are a special type of container, specifically designed to operate with sequences of characters.
Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers.
The string class is an instantiation of the basic_string class template, defined in <string> as:;
typedef basic_string<char> string
You can/should use string to store input, but can/should use char when you need to manipulate the characters in a string of input?
As mentioned before, consider a string as an array of characters, so the same rules essentially apply. Let's say you have the following code:
1 2
string sentence = "This is a sentence";
cout << sentence[3];
You can access any element in the string by using the subscript operator just as you would in a char array. In the example I am outputting the character the is stored in element 3 of the string. Since arrays start indexing at 0, position 3 is the character 's'.
Also, your code has some problems. Try to compile it and see if you can find the errors first.