strings intro
First, C++ string objects associate the array of characters which constitute the string with methods useful for managing and operating on it, a C++ string object knows its starting location in memory, its content, its length in characters, and the length in characters to which it can grow before the string object must resize its internal data buffer.
difference between C char arrays and C++ strings. C++ strings do not include a null terminator, nor do the C++ string handling member functions rely on the existence of a null terminator to perform their jobs. C++ strings greatly reduce the likelihood of making three of the most common and destructive C programming errors:
1.overwriting array bounds
2. trying to access arrays through uninitialized or incorrectly valued pointers,
3. and leaving pointers “dangling” after an array ceases to occupy the storage that was once allocated to it.
Example :
#include <string> #include <iostream> using namespace std; int main() { string s1("12345"); string s2 = s1; // copy of string s1 cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; getchar(); return 0; }