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
:
1 2 3 |
template< class CharT, class Traits = std::char_traits<CharT> > class basic_string_view; |
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.
Type | Char Type | Definition | Standard | |
std::string_view | char | std::basic_string_view<char> | (C++17) | |
std::wstring_view | wchar_t | std::basic_string_view<wchar_t> | (C++17) | |
std::u8string_view | char8_t | std::basic_string_view<char8_t> | (C++20) | |
std::u16string_view | char16_t | std::basic_string_view<char16_t> | (C++17) | |
std::u32string_view | char32_t | std::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.
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 |
#include <iostream> #include <string> #include <string_view> int main() { const std::string_view sview = "This is a stringview example"; const std::wstring_view wsview = L"This is a stringview example"; const std::u16string_view u16sview = u"This is a stringview example"; const std::u32string_view u32sview = U"This is a stringview example"; const std::string_view sviews[]{ "This is ", "LearnCPlusPlus.org ", "and welcome ", "!" }; for ( auto sv : sviews) { std::cout << sv << " | "; } std::cout << std::endl; std::cout << sview[1] << std::endl; // view second char with operator[] std::cout << sviews[1] << std::endl; // view second string with operator[] std::cout << "size:" << sview.size() << std::endl; std::cout << "length:" << sview.length() << std::endl; system("pause"); return 0; } |
This is the output that shows how we can use it with single string or with multiple string arrays.
1 2 3 4 5 6 7 |
This is | LearnCPlusPlus.org | and welcome | ! | h LearnCPlusPlus.org size:28 length:28 |
For more details about this feature in C++17 standard, please see this P0220R1, P0254R2, P0403R1
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition