The Standard Class string

Defining And Assigning Strings

Initializing

string message = "Good Morning!"; 
 
Note 
Objects of class string do not necessarily contain the string terminating character '\0', as is the case with C strings.

Sample program

// string1.cpp: Using strings
#include <iostream>
#include <string>
using namespace std;
string prompt("Enter a line of text: "),    // Global
       line( 50, '*');                      // strings
int main()
{
   string text;                      // Empty string
   cout << line << endl << prompt << endl;
   getline( cin, text);        // Reads a line of text
   cout << line << endl
        << "Your text is " << text.size()
        << " characters long!" << endl;
                                 // Two new strings:
   string copy(text),            // a copy and the
          start(text,0,10);      // first 10 characters
                                 // starting with
                                 // position 0.
   cout << "Your text:\n" << copy << endl;
   text = "1234567890";          // Assignment
   cout << line << endl
        << "The first 10 characters:\n" << start << endl
        << text << endl;
   return 0;
} 
C++ uses the standard class string to represent and manipulate strings allowing for comfortable and safe string handling. During string operations the required memory space is automatically reserved or modified. The programmer does not need to concern himself or herself with internal memory allocation.
The string class is defined in the string header file and was mentioned in Chapter 3 as an example for the use of classes. Several operators are overloaded for strings, that is, they were also defined for the string class. This allows for easy copying, concatenation, and comparison. Additionally, various methods for string manipulation such as insertion, erasing, searching, and replacing are available.

Initializing Strings

A string, that is, an object belonging to the string class, can be initialized when you define it using
  • a predefined string constant
  • a certain number of characters
  • a predefined string or part of a string.
If a string is not initialized explicitly, an empty string with a length of 0 is created. The length of a string, that is, the current number of characters in the string, is stored internally and can be accessed using the length() method or its equivalent size().
Example:
string message("Good morning!");
cout << message.length();   // Output: 13 

String Assignments

When you assign a value to a string, the current contents are replaced by a new character sequence. You can assign the following to a string object:
  • another string
  • a string constant or
  • a single character.
The memory space required is adjusted automatically.
The program on the opposite page uses the function getline(), which was introduced in an earlier chapter, to store a line of text from the keyboard in a string. In contrast, the >> operator reads only one word, ignoring any leading white space. In both cases the original content of the string is lost.

Post a Comment

0 Comments