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 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: template 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 (C++17) std::wstring_view wchar_t std::basic_string_view (C++17) std::u8string_view char8_t std::basic_string_view (C++20) std::u16string_view char16_t std::basic_string_view (C++17) std::u32string_view char32_t std::basic_string_view (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 #include #include 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
