Core C++

Lesson 46 : Using STL Strings

In this C++ video tutorial, we demonstrate how to use STL strings to perform common string operations, such as input, get length, copy, concatenation, tokenize, and compare. STL strings offer numerous advantages over the simpler array strings that we have used previously. Some of these advantages are apparent now such as automatic resizing, and some will become clear later when we have covered more material on C++ templates. All of these together, make STL string an obvious preference and we will work with them in the future wherever possible.

There are two types of STL strings, for which we must include <string>: string and wstring. The string type holds ASCII encoded characters; these are simply chars. The wstring type holds Unicode encoded characters; these are wchar_t type characters. Internally, each of these types holds an array of chars or wide chars, which resizes automatically. We can access the array via the c_str() function.

#include <iostream>
#include <string>
#include <sstream>

int main() {
    using namespace std;

    stringstream qStringToDouble;
    double dConvertedDouble;
    string qPi("3.14159");

    // Conversion using stringstream
    qStringToDouble << qPi;
    qStringToDouble >> dConvertedDouble;
    cout << "Stream conversion = " << dConvertedDouble << endl;

    // Conversion using the old array-based method
    dConvertedDouble = atof(qPi.c_str());
    cout << "Array conversion = " << dConvertedDouble << endl;

    return 0;
}

In the program above, we demonstrate how to convert a string of digits into a double value. The first section of code demonstrates how to do the conversion by using the C++ based stringstream. In the second section, we call c_str() to get the char array and call atof() to make the conversion as we would with an array string.

Here, we have the output of the two converted doubles. This demonstrates the versatility of the STL string type that we can still use array-based functions with them.

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    using namespace std;

    const int kiPoemCount = 4;

    // An array of Robert Frost's poems
    string qaFrostPoems[kiPoemCount] = {
        "The Road Not Taken",
        "Mending Wall",
        "Reluctance",
        "The Tuft of Flowers"};

    cout << "Unsorted List:" << endl;
    cout << "--------------" << endl;
    for (int iIndex = 0; iIndex < kiPoemCount; ++iIndex) {
        cout << qaFrostPoems[iIndex] << endl;
    }

    // Sort the array
    string* qpStart = &(qaFrostPoems[0]);
    string* qpEnd = &(qaFrostPoems[kiPoemCount]);
    sort(qpStart, qpEnd);

    cout << endl << "Sorted List:" << endl;
    cout << "------------" << endl;
    for (int iIndex = 0; iIndex < kiPoemCount; ++iIndex) {
        cout << qaFrostPoems[iIndex] << endl;
    }
}

The program above demonstrates how to sort an array of strings. The sort() function can be found in the <algorithm> header, which contains many other useful functions that we will explore later. We begin with an array of the titles of four of Roberts Frost's poems: The Road Not Taken, Mending Wall, Reluctance, and The Tuft of Flowers.

Here, we see the two outputted lists: the unsorted and the sorted. Before the sort, we see the unsorted list of Robert Frost's poems like this:

  1. The Road Not Taken
  2. Mending Wall
  3. Reluctance
  4. The Tuft of Flowers

After the sort is called, we see Robert Frost's poems sorted like this:

  1. Mending Wall
  2. Reluctance
  3. The Road Not Taken
  4. The Tuft of Flowers
 

© 2007–2024 XoaX.net LLC. All rights reserved.