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
Technical details
Technical definition of a string view
Named requirements
Feature testing macros
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
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.