Note, this article is not finished! You can help by editing this doc page.
String view class reference
Overview
- Simplified
- Detailed
template< class CharT, /* ... */ >
class basic_string;
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_string;
A class that is used as a lightweight view over a sequence of characters.
Memory
This section requires improvement. You can help by editing this doc page.
Aliases
Type std::string_view is in fact an alias to: std::basic_string_view<char>.
Available std::basic_string_view aliases
| pub | std::string_view | std::basic_string_view<char> |
| pub | std::wstring_view | std::basic_string_view<wchar_t> |
| pub | std::u8string_view (since C++20) | std::basic_string_view<char8_t> |
| pub | std::u16string_view (since C++11) | std::basic_string_view<char16_t> |
| pub | std::u32string_view (since C++11) | std::basic_string_view<char32_t> |
Technical details
Technical definition of a string view
The class template basic_string_view describes an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero.
A typical implementation holds only two members: a pointer to constant CharT and a size.
std::basic_string_view is a TriviallyCopyable type. (since C++11)Named requirements
std::basic_string_view meets the requirements of:
-
TriviallyCopyable (since C++23)
-
note
Specializations of
std::basic_string_vieware already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.
-
Feature testing macros
This section requires improvement. You can help by editing this doc page.
std::string_view
| Defined in | string_view |
Template parameters
| pub | CharT | Character type |
| pub | Traits |
important Like for |
Type names
| pub | traits_type | Traits |
| pub | value_type | CharT |
| pub | pointer | CharT* |
| pub | const_pointer | const CharT* |
| pub | reference | CharT |
| pub | const_reference | const CharT& |
| pub | const_iterator | implementation-defined constant |
| pub | const_reverse_iterator | std::reverse_iterator<const_iterator> |
| pub | reverse_iterator | const_reverse_iterator |
| pub | size_type | std::size_t |
| pub | difference_type | std::ptrdiff_t |
iterator and const_iterator are the same type because string views are views into constant character sequences.
All requirements on the iterator types of a Container applies to the iterator and const_iterator types of std::basic_string_view as well.
Member functions
| pub | (constructors) | Constructs a string view. |
| pub | (destructor) | Destroys the string view. |
| pub | operator= | Assigns values to the string string view. |
Element access
| pub | at | Accesses the specified character with bounds checking. |
| pub | operator[] | Accesses the specified character. |
| pub | front | Returns the first character. |
| pub | back | Returns the last character. |
| pub | data | Returns a pointer to the first character of a view. |
Iterators
| pub | begin cbegin | Returns an |
| pub | end cend | Returns an |
| pub | rbegin crbegin | Returns a reverse |
| pub | rend crend | Returns a reverse |
Capacity
| pub | empty | Returns |
| pub | size length | Returns the number of characters. |
| pub | max_size | Returns the maximum number of characters. |
Modifiers
| pub | remove_prefix | Shrinks the view by moving its start forward. |
| pub | remove_suffix | Shrinks the view by moving its end backward. |
| pub | swap | Swaps the contents. |
Operations
| pub | copy | Copies characters. |
| pub | substr | Returns a substring. |
| pub | compare | Compares two views. |
| pub | starts_with (since C++20) | Checks if the view starts with the given prefix. |
| pub | ends_with (since C++20) | Checks if the view ends with the given suffix. |
| pub | contains (since C++23) | Checks if the view contains the given substring or character. |
| pub | find | Find the first occurrence of a substring. |
| pub | rfind | Find the last occurrence of a substring. |
| pub | find_first_of | Find first occurrence of characters. |
| pub | find_first_not_of | Find first absence of characters. |
| pub | find_last_of | Find last occurrence of characters. |
| pub | find_last_not_of | Find last absence of characters. |
Constants
| pubstaticconstexpr | npos | A special value. The exact meaning depends on the context. |
Non-member functions
| pub | operator== operator!= (removed in C++20) operator< (removed in C++20) operator> (removed in C++20) operator<= (removed in C++20) operator>= (removed in C++20) operator<=> | Lexicographically compares two string views. |
Input/output
| pub | operator<< | Performs stream output on views. |
Literals
Defined in namespacestd::literals::string_view_literals| pub | operator ""sv | Creates a string view out of a character literal. |
Helper classes
| pub | std::hash<std::string_view> (since C++11) std::hash<std::wstring_view> (since C++11) std::hash<std::u8string_view> (since C++20) std::hash<std::u16string_view> (since C++11) std::hash<std::u32string_view> (since C++11) | Specializations for |
Specializations
- since C++20
template<class CharT, class Traits>
inline constexpr bool ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>> = true;
This specialization of ranges::enable_borrowed_range makes basic_string_view satisfy borrowed_range.
- since C++20
template<class CharT, class Traits>
inline constexpr bool ranges::enable_view<std::basic_string_view<CharT, Traits>> = true;
This specialization of ranges::enable_view makes basic_string_view satisfy view.
Deduction guides (since C++20)
Click to expand
// (1)
template<class It, class End>
basic_string_view(It, End) -> basic_string_view<std::iter_value_t<It>>;
(1)allows deduction from an iterator-sentinel pair.
// (2)
template<class R>
basic_string_view(R&&) -> basic_string_view<ranges::range_value_t<R>>;
(2)allows the character type to be deduced from an arbitrary range.
Overload resolution
In order for any of the deduction guides to participate in overload resolution, the folllowing requirements must be met:
- for
(1),Itsatisfiescontiguous_iteratorandEndsatisfiessized_sentinel_forforIt - for
(2),Rsatisfiescontiguous_range
Examples
Basic manipulation
#include <iostream>
#include <string>
int main()
{
std::string s = "World";
std::cout << "Hello, " << s << "!" << std::endl;
}
Hello, World!
#include <iostream>
#include <string>
int main()
{
std::string a = "Hello, ";
std::string b = "World!";
std::string c = a + b;
std::cout << c;
}
Hello, World!
#include <iostream>
#include <string>
int main()
{
// Split by the comma.
// v
std::string a = "Hello, World!";
size_t pos = a.find(',');
std::string hello = a.substr(0, pos);
// 1 for a comma, and 1 for a space before "World"
std::string world = a.substr(pos + 1 + 1);
std::cout << hello << '\n' << world;
}
Hello
World!
Conversions
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "8314";
// Note: stoi can throw exception if failed!
int number = std::stoi(numberInString);
std::cout << "Number: " << number;
}
Number: 8314
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "3.141";
// Note: stof can throw exception if failed!
float number = std::stof(numberInString);
std::cout << "Number: " << number;
}
Number: 3.141
Convert numbers to std::string
#include <iostream>
#include <string>
int main()
{
std::string numberInString = "3.141";
// Note: stof can throw exception if failed!
float number = std::stof(numberInString);
std::cout << "Number: " << number;
}
s1: 123
s2: 456.200012
s3: 3.141590
Hover to see the original license.