Interface for the Standard C++ Class string

  1. C++ compilers that conforms to the ANSI C++ Standard has the standard string class.

  2. The standard string class allows one to perform the same operations as can be done with cstrings, but in easier and less error prone ways.

  3. The syntax for declaring an object of type string is exactly as the way an object of a class is declared:

    string string_name;

  4. The class string has an overloaded constructor that takes a cstring argument. It initializes the string object to a value given by the cstring argument. It makes the string class compatible with the cstrings.

  5. The string class overloads the = operator to copy one string of any length into another.

  6. The relation == is overloaded to compare two strings.

  7. The operators >> and << are overloaded to perform input and output. Operator >> reads up to the next whitespace but does not consume that whitespace. Reading input using >> actually reads words (a sequence of non-whitespace characters) and not lines.

  8. To illustrate the usage of some of the features about the string class, consider the following code:

    string phrase, word1("hot"), word2("dog");
    phrase = word1 + word2;
    cout << phrase;

    The first line declares a string object called phrase, and then declares and initializes a string word1 to "hot" and word2 to "dog". The latter two strings are concatenated (joined together) by the overloaded operator "+", and assigned to the string phrase. The result is then output to the screen as:

    hotdog

  9. The string class overloads the square brackets [ ] so that they can be used to access individual characters at any position of the string. For example:

    string LastName("Rodham");
    cout << "The third letter of the name is: \'" << LastName[2] << "\', not a \'t\'.\n";

    will produce the output:
    The third letter o the name is: 'd', not a 't'.

  10. Unfortunately this index overloading in the string class does not do range-checking, i.e., it does not check to see if an illegal index is used. It is better to use the string class member function at, which does range-checking. For example:

    string LastName("Rodham");
    LastName.at(2) = 't';
    cout << "The name is changed to: " << LastName << ;

    will produce the following output:
    The name is changed to: Rotham

  11. The member function length( ) gives the length (number of characters) of the string. It corresponds to the cstring function strlen in the case of cstrings.

  12. With cstrings, to read an entire line, the istream member function getline can be used. With string objects, to read an entire line, a function also called getline can be used.

  13. However, this getline function is not a member function of any class. It is a stand alone function whose first argument is an istream object, second argument is a string object, and third argument (if present) is a delimiter character signifying the end of the input.

  14. The getline function reads characters from the istream object, inserts the characters into the string variable until the delimiter character is encountered. The delimiter character is removed from the input and discarded. In the absence of a delimiter character, the default character '\n' is used.

  15. //Demonstrates the class string.

    #include <iostream>
    #include <string>
    using namespace std;

    void new_line( );

    int main( )
    {
        string first_name, last_name, record_name;

        cout << "Enter your first and last name:\n";

        cin >> first_name >> last_name;
        new_line( );

        record_name = last_name + ", " + first_name;

        cout << "Your name in our records is: ";
        cout << record_name << endl;
        cout << "Your last name is spelled: ";

        for (int i = 0; i < last_name.length( ); i++)
            cout << last_name[i] << " ";
        cout << endl;

        string motto("Your records are our records.");
        cout << "Our motto is\n";
        cout << motto << endl;
        cout << "Please suggest a better (one line) motto:\n";

        getline(cin, motto);
        cout << "Our new motto will be:\n";
        cout << motto << endl;

        return 0;
    }

    void new_line( )
    {
        char symbol;
        do
        {
            cin.get(symbol);
        } while (symbol != '\n');
    }

  16. In the above program, after cin was used to read the last name and first name from the keyboard, the newline character '\n' remains in the standard input stream and is not consumed. A subsequent input from the standard input stream using getline will read '\n', as though the user had entered nothing and just hit the return key. The program handles that by using the function new_line to get rid of the newline character, as well as any other characters that remain in the stream before the newline character.

  17. It turns out that the istream library has a powerful member function called ignore that can be used to accomplish the above task. Its prototype is

    istream& ignore ( int so_many, char delimiter);

  18. The ignore member function reads up to so_many characters, or until the delimiter character is encountered, which ever is first, and discards these characters.

  19. The ignore function can be used to eliminate the '\n' character left after a cin << variable statement, so that a following getline will not get an empty string.