Im new to C++.. Wondering what "string" is and what its used for?
Printable View
Im new to C++.. Wondering what "string" is and what its used for?
A string is a type which holds any sequence of characters, typically a word or sentence. std::string is a robust implementation of the concept in the C++ standard library.
In general, a string is a line of text. Example: "This is a string"
In C, there are no strings. Instead, you use an array of individual characters. The last value in the array is always 0 (null) which marks the end of the string. Example:
char[] name = "Chris";
which is actually an array of type char: 'C', 'h', 'r', 'i', 's', '\0'
In C++, a new typedef is added called std::string, which is actually a template class (basic_string<T>) being called as basic_string<char>.
It's basically a template that wraps your normal character array and gives you an easier interface to work with.
To use a string in a function that reacquires a char[] all you have to do is:
void printname(const char* name);
string name = "Chris";
printname( name.c_str() );
Despite that you will find tons of resources using your favorite search engine....Wikipedia is your friend: string (C++)... ;)