Site icon Learn 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 :

[crayon-66064efd1b740191006653/]

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

[crayon-66064efd1b747478610588/]

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++,

[crayon-66064efd1b748498539314/]

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

[crayon-66064efd1b74a749929438/]

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

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

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++,

[crayon-66064efd1b74c324520391/]

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

[crayon-66064efd1b750006625021/]

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

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

[crayon-66064efd1b751762556475/]

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.

Exit mobile version