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 :
1 2 3 4 5 6 7 |
template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_string; |
Since C++17 it is also defined in a pmr namespace as below,
1 2 3 4 5 6 7 |
namespace pmr { template <class CharT, class Traits = std::char_traits<CharT>> using basic_string = std::basic_string< CharT, Traits, std::polymorphic_allocator<CharT> >; } |
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++,
1 2 3 4 5 6 |
std::basic_string<char> str1 = "This is a String"; std::basic_string<wchar_t> str2 = L"This is a String"; std::basic_string<char16_t> str3 = u"This is a String"; std::basic_string<char32_t> str4 = U"This is a String"; |
Instead of these use std string, wstring, u16string, u32string data types, which are more general usage. These below are the same as above,
1 2 3 4 5 6 |
std::string str1 = "This is a String"; std::wstring str2 = L"This is a String"; std::u16string str3 = u"This is a String"; std::u32string str4 = U"This is a String"; |
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++,
1 2 3 4 5 6 |
std::pmr::basic_string<char> str1 = "This is a String"; std::pmr::basic_string<wchar_t> str2 = L"This is a String"; std::pmr::basic_string<char16_t> str3 = u"This is a String"; std::pmr::basic_string<char32_t> str4 = U"This is a String"; |
Instead of these use pmr string, wstring, u16string, u32string data types, which are more general. These below are the same as above,
1 2 3 4 5 6 |
std::pmr::string pstr1 = "This is a String"; std::pmr::wstring pstr2 = L"This is a String"; std::pmr::u16string pstr3 = u"This is a String"; std::pmr::u32string pstr4 = U"This is a String"; |
Is there a full modern C++ example of how to use basic_string?
Here is a full C++ example about the basic_string,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> int main() { std::basic_string<char> str1 = "This is a String"; std::basic_string<wchar_t> str2 = L"This is a String"; std::basic_string<char16_t> str3 = u"This is a String"; std::basic_string<char32_t> str4 = U"This is a String"; /* same with: std::string str1 = "This is a String"; std::wstring str2 = L"This is a String"; std::u16string str3 = u"This is a String"; std::u32string str4 = U"This is a String"; */ std::pmr::basic_string<char> pstr1 = "This is a String"; std::pmr::basic_string<wchar_t> pstr2 = L"This is a String"; std::pmr::basic_string<char16_t> pstr3 = u"This is a String"; std::pmr::basic_string<char32_t> pstr4 = U"This is a String"; /* same with: std::pmr::string pstr1 = "This is a String"; std::pmr::wstring pstr2 = L"This is a String"; std::pmr::u16string pstr3 = u"This is a String"; std::pmr::u32string pstr4 = U"This is a String"; */ return 0; } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition