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

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

What is basic_string in modern C++? How can I use basic_string in C++? Is std::basic string same as std::string?

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 type that 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 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::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 ::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.


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++ComponentsLanguage FeatureLearn C++

How To Add Shadow Effects To Your C++ Apps

C++ComponentsLanguage FeatureLearn C++

How To Make Controls Have A Glow Effect In C++?

C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

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

This Is How To Use Parallel Programming in C++ Builder