C++C++11C++14C++17Learn C++

What You Need To Know About std::basic_string In Modern C++

In this post, you’ll learn what a basic string is in modern C++ and how to use it. Is std::basic string equivalent to std::string? By learning more about std::basic string, it will help you to easily build C++ applications using the C++ IDE.

What is basic_string?

The basic_string (std::basic_string and std::pmr::basic_string) is a class template that stores and manipulates sequences of alpha numeric string objects (char,w_char,…). For example, str::string and std::wstring are the data types defined by the std::basic_string<char>. In other words, basic_string is used to define different data_types which means a basic_string is not a string only, it is a namespace for a general string format. A basic string can be used to define string, wstring, u8string, u16string and u32string data types.

The basic_string class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter (i.e. a specialization of std::char_traits) or a compatible traits class. The basic_string  stores the elements contiguously.

Several string types for common character types are provided by basic string definitions as below,

String TypeBasic String DefinitionStandard
std::string std::basic_string<char>
std::wstringstd::basic_string<wchar_t>
std::u8stringstd::basic_string<char8_t>(C++20)
std::u16stringstd::basic_string<char16_t>(C++11)
std::u32stringstd::basic_string<char32_t> (C++11)

Several string type in std::pmr namespace for common character types are provided by the basic string definitions as below,

String Type Basic String Definition Standard
std::pmr::string std::pmr::basic_string<char>(C++17)
std::pmr::wstringstd::basic_string<wchar_t>(C++17)
std::pmr::u8stringstd::basic_string<char8_t>(C++20)
std::pmr ::u16stringstd::basic_string<char16_t>(C++17)
std::pmr ::u32stringstd::basic_string<char32_t> (C++17)

What is the syntax of basic_string?

Here is the syntax of the basic_string,

Syntax :

Since C++17 it is also defined in a pmr namespace as below,

Is there a simple example of how to use std::basic_string?

Here is a simple example that shows how you can use basic_string in modern C++,

Instead of these use std string, wstring, u16string, u32string data types, which are more general usage. These below are the same as above,

as you see different string data types requires different ‘L’,’u’ and ‘U’ literals.

L, u and U are String Literals here, represents the type of characters of string. These might be default in your editor and/or compiler options which means you don’t need to add if you know the default. A string literal is a sequence of characters surrounded by double quotes, optionally prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in “…”, R”(…)”, u8″…”, u8R”(…)“, u”…”, uR”˜(…)˜”, U”…”, UR”zzz(…)zzz”, L”…”, or LR”(…)”, respectively. Please see the “String Literals” section in this document Working Draft, Standard for Programming Language C++. Here below we list some of these standards used in C++.

Examples for String Literals for Strings Definitions

  • str=”abcd”; default string based on compiler/IDE options.
  • str=u8″abcd”; a UTF-8 string literal and is initialized with the given characters as encoded in UTF-8, including the null terminator
  • str=u”abcd”; a char16_t string literal. A char16_t string literal has type “array of n const char16_t”, including the null terminator
  • str=U”abcd”; a char32_t string literal. A char32_t string literal has type “array of n const char32_t”, including the null terminator
  • str=L”abcd”; a wide string literal. A wide string literal has type “array of n const wchar_t”, including the null terminator
  • str=R”abcd”; raw strings

What is the difference between L”” and U”” and u”” prefix literals in C++?

  • L is based on wide string literal depends on array of n const wchar_t in your compiler/IDE options. Generally it is UTF-8 or UTF-16 format
  • u is for UTF-16 format,
  • U is for UTF32 formats

Is there a simple example of how to use std::pmr::basic_string?

Here is a simple example that shows how you can use std::pmr::basic_string in modern C++,

Instead of these use pmr string, wstring, u16string, u32string data types, which are more general. These below are the same as above,

Is there a full modern C++ example of how to use basic_string?

Here is a full C++ example about the basic_string,

What are the String and UnicodeString keywords?

Let’s refresh our memories on the use of String and UnicodeString. Click on the link below to see the article.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn About Deleted Implicit Destructors in A C++ App

C++C++11C++14C++17Introduction to C++Learn C++

Learn About Disabling The Implicit Destructor In A C++ App

C++C++11C++14C++17Introduction to C++Learn C++

What Is An Implicitly Declared Destructor In A C++ App?

C++Introduction to C++Language FeatureLearn C++

How To Convert An Integer To A String In A C++ App