Hello, I am kinda new to understanding string objects and I am not sure if I actually do understand them. So here's my questions, are strings simply c strings but words instead of letters and no null characters? Can it be coded simply, as I will have done below, as a cin >> statement? and have a loop to where the string can be filled with separated objects within the string?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//just in case of confusion
usingnamespace std;
//declaration of the string obj
string randoInput[10];
for (int x = 0; i<10; i++)
{
cin >> randoInput[i];
//or
cin >> randoInput[i];
getline(cin, randomInput;
}
and then within these strings, are each element understood the same as an array would be ? (randoInput[x])
1) No, they are not C strings. Not sure what you mean ... neither one has a concept of 'words'. both are containers of letters. You can have null characters in string objects, but it is not the end of string marker, its effectively a space when printed on most systems.
2) it can be coded simply, yes. string x = "words and stuff"; cin >> x; cout << x; etc all work straight up.
3) you made an array of string objects. this is on par with char ** not char* or char[][] not char[] ... that isnt a string, exactly.
4) you can fill your array of strings in a loop, yes. But its not a string, its an array of strings.
5) string x = "words"; x[0] is the letter 'w'.
Its ok to have an array of strings, but one string object is like an array of char. I think this is what you do not understand?
for your array, randoInput[0] is the first STRING OBJECT. randoInput[0][0] is the first LETTER of the first STRING OBJECT. randoInput[0][1] is the second letter, and so on. randoInput[1][0] is the second 'word' or 'string object'. All this is because you made an array of strings, though. Its YOUR construct that extends the string objects to a second dimension that could mean 'words' (does not have to mean that, but it could)
Strings are objects that represent sequences of characters.
A C-string is a sequence of characters that has null char at end to indicate the end of the sequence. In other words, the length is encoded inside the data.
A std::string knows the length of its sequence (it has member function size()). Therefore, it does not depend on the null.
The unformatted input with std::getline reads a sequence of characters up to delimiter character that by default is newline (You could think the null a delimiter of C-string). http://www.cplusplus.com/reference/string/string/getline/