Site icon Learn C++

What Is basic_string_view And string_view In Modern C++

What Is basic string view And string view In Modern C++

C++17 had enormous changes in C++ features. One of them was basic_string_view (std::basic_string_view) which is a constant string container that can be used for multiple string declarations. The basic_string_view is a modern way of read-only text definition. It can be used by iterators and other methods of the basic_string_view class. In this post, we explain basic_string_view and its types.

What is basic_string_view in modern C++

In the Library Fundamentals Technical Specification (LFTS), The C++ commitee introduced std::basic_string_view, and it was approved for C++17. The basic_string_view (std::basic_string_view) is a class template defined in the <string_view> header that is used to define a constant string container that can be used to define multiple strings which refers to the contiguous character sequence, and the first element of this sequence position at the zero position. The basic_string_view is a modern way of read-only text definition, it can be used by iterators, and other methods of the basic_string_view class can be used. 

Here is the syntax of the basic_string_view:

[crayon-676419adc3ffb425861645/]

We can use different character types in std::basic_string_view types as same as std::basic_string. The std::basic_string_view has 5 different string types, these are, std::string_view, std::wstring_view, std::u8string_view, std::u16string_view, and std::u32string_view.

The std::string_view provides read-only access to a string definition with chars as similar to the interface of std::string. In addition to this, we can use std::wstring_view, std::u16string_view, and std::u32string_view in C++17. There is std::u8string_view that is added by the C++20 standards too.

Here are the basic_string_types and their features.

TypeChar TypeDefinitionStandard
std::string_viewcharstd::basic_string_view<char>(C++17)
std::wstring_viewwchar_tstd::basic_string_view<wchar_t>(C++17)
std::u8string_viewchar8_tstd::basic_string_view<char8_t>(C++20)
std::u16string_viewchar16_tstd::basic_string_view<char16_t>(C++17)
std::u32string_viewchar32_tstd::basic_string_view<char32_t>(C++17)

Is there an example about basic_string_view in modern C++

Here is a C++ example that we use std::string_view, std::wsting_view, std::u16string_view, std::u32string_view in C++17.

[crayon-676419adc4009093382177/]

This is the output that shows how we can use it with single string or with multiple string arrays.

[crayon-676419adc4014164060706/]

For more details about this feature in C++17 standard, please see this P0220R1P0254R2P0403R1

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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