Chapter Overview

Get an idea of what we'll cover in this chapter.

We'll cover the following...

The STL string class is a powerful, full-featured tool for storing, manipulating, and displaying character-based data. It has much of the convenience we would find in a high-level scripting language, yet remains as quick and agile as we would expect from C++.

The string class is based on basic_string, a contiguous containerContiguous containers refer to data structures in programming that store elements in a contiguous block of memory, allowing for efficient random access and element traversal due to their memory layout. class that may be instantiated with any character type. Its class signature looks like this:

template<
typename CharT,
typename Traits = std::char_traits<CharT>,
typename Allocator = std::allocator<CharT>
> class basic_string;

Note: The Traits and Allocator template parameters are usually left to their default values.

The underlying storage of basic_string is a contiguous sequence of CharT, and can be accessed with the data() member function:

const std::basic_string<char> s{"hello"};
const char * sdata = s.data();
for(size_t i{0}; i < s.size(); ++i) {
cout << sdata[i] << ' ';
}
cout << '\n';

Output:

h e l l o

The data() member function returns a CharT* that points to the underlying array of characters. Since C++11, the array returned by data() is null-terminated, making data() equivalent to c_str().

The basic_string class includes many of the methods we would find in other contiguous-storage classes, including insert(), erase(), push_back(), pop_ back(), and others. These methods operate on the underlying array of CharT.

std::string is a type alias for std::basic_string<char>:

using std::string = std::basic_string<char>;

For most purposes, we'll use std::string.

String formatting

String formatting has traditionally been a weak point with the STL. Until recently, we've been left with an imperfect choice between the cumbersome STL iostreams or the archaic legacy printf(). Beginning with C++20 and the format library, STL string formatting has finally grown up. Closely based on Python's str.format() method, the new format library is fast and flexible, providing many of the advantages of both iostreams and printf(), along with good memory management and type safety.

While we no longer need to use iostreams for string formatting, it is still quite useful for other purposes, including file and stream I/O, and some type conversions.

In this chapter, we will cover these subjects and more in the following recipes:

  • Use string_view as a lightweight string object
  • Concatenate strings
  • Transform strings
  • Format text with C++20’s format library
  • Trim whitespace from strings
  • Read strings from user input
  • Count words in a file
  • Initialize complex structures from file input
  • Customize a string class with char_traits
  • Parse strings with Regular Expressions