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.
Table of Contents
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 Type | Basic String Definition | Standard |
std::string | std::basic_string<char> | |
std::wstring | std::basic_string<wchar_t> | |
std::u8string | std::basic_string<char8_t> | (C++20) |
std::u16string | std::basic_string<char16_t> | (C++11) |
std::u32string | std::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::wstring | std::basic_string<wchar_t> | (C++17) |
std::pmr::u8string | std::basic_string<char8_t> | (C++20) |
std::pmr ::u16string | std::basic_string<char16_t> | (C++17) |
std::pmr ::u32string | std::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.