C++ 11 Strings

In this tutorial, you will be presented with the string types of the C++ standard library. This portrays the fundamental class template basic_string<> along with the standard specializations like string, wstring, u16string, and u32string. Here, you will learn about the advanced concepts of handling strings that are being introduced with the C++11 standard.

Strings might be a reason of confusion as it is not clear what actually the string means. It remains as a confusion whether strings are type char*. Is it an example of class string<>; or acts as a general nomenclature of objects which are kinds of strings. In this tutorial, you will use the phrase 'string' for objects of the string categories in the C++ standard library:

  • String
  • Wstring
  • u16string

Strings on C++98

It is to be noted that with C++98 the type of string literals (like "Welcome") was altered to the form - const char*. Though, for providing backward compatibility, you can implement an implicit yet deprecated conversion to char*. But in the real sense, the original type of literal such as "Welcome" is const char[8]. But this type converts automatically to const char* Yet, while working with templates, the variation might matter as for reference template parameters, decompose does not occur if not type trait std::decay() is used.

Recent Changes with C++11

C++98 have precisely all features and characteristics of string classes. Below mentioned are the lists of the main important features that are included new with C++11:

  • Strings can supply front() and back() functions for accessing the starting and / or ending element and shrink_to_fit() method for shrinking the capacity.
  • Strings also allow handiness functions for converting strings to numeric values and the back to strings.
  • Functions like data() and c_str() no longer nullify references, pointers to strings and iterators.
  • Now strings can support move semantics along with initializer lists.
  • Other than string and wstring, the basic_string<> specializations u16string along with u32string are now predefined.
  • Strings can now in some way necessary for providing an end-of-string character which you usually do like this - '\0' for string; since for a string g, g[g.length()] is at all times valid and g.data() returns the characters that includes an end-of-string character.

Simple Example for extracting words and prints those words backward

The code is given below take out single words from standard input (command-line program) and prints the characters of each word in reverse order. The words are divided by the usual whitespaces (newline, space, and tab) and by commas, periods, or semicolons:

Example:
#include<iostream> 
#include<string> 

void main(int argc, char** argv)
{
    const string delim(" \t");
    string l;
    while (getline(cin, l)) {
        string::size_type beg, end;
        // search starting of the 1st word
        beg = line.find_first_not_of(delim);
        while (beg != string::npos) {
            // searches for end of the actual word
            end = l.find_first_of(delim, beg);
            if (end == string::npos) {
                // end of word is the end of line
                end = line.length();
            }
            // print characters in reverse order
            for (int i = end - 1; i >= static_cast(beg); --i) {
                cout << l[i];
            }
            cout <<'  ';
            // searches for beginning of the next word
            beg = l.find_first_not_of(delim, end);
        }
        cout << "\n";
    }
}

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.