Przejdź do głównej zawartości
uwaga

Note, this article is not finished! You can help by editing this doc page.

String view class reference

Overview

template< class CharT, /* ... */ >
class basic_string;

A class that is used as a lightweight view over a sequence of characters.

Memory

important

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
pubstd::string_viewstd::basic_string_view<char>
pubstd::wstring_viewstd::basic_string_view<wchar_t>
pubstd::u8string_view (od C++20)std::basic_string_view<char8_t>
pubstd::u16string_view (od C++11)std::basic_string_view<char16_t>
pubstd::u32string_view (od 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.

Every specialization of std::basic_string_view is a TriviallyCopyable type.  (od C++11)
Named requirements

std::basic_string_view meets the requirements of:

  • TriviallyCopyable (od C++23)
    • zanotuj

      Specializations of std::basic_string_view are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.

Feature testing macros
important

This section requires improvement. You can help by editing this doc page.

std::string_view

Defined instring_view

Template parameters

pubCharTCharacter type
pubTraits

CharTraits class specifying the operations on the character type.

important

Like for std::basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed

.

Type names

pubtraits_typeTraits
pubvalue_typeCharT
pubpointerCharT*
pubconst_pointerconst CharT*
pubreferenceCharT
pubconst_referenceconst CharT&
pubconst_iterator

implementation-defined constant LegacyInputIterator, ConstexprIterator  (od C++20) and LegacyContiguousIterator  (do C++20) contiguous_iterator  (od C++20) whose value_type is CharT

pubconst_reverse_iteratorstd::reverse_iterator<const_iterator>
pubreverse_iteratorconst_reverse_iterator
pubsize_typestd::size_t
pubdifference_typestd::ptrdiff_t
Iterators

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.

puboperator=

Assigns values to the string string view.

Element access

pubat

Accesses the specified character with bounds checking.

puboperator[]

Accesses the specified character.

pubfront

Returns the first character.

pubback

Returns the last character.

pubdata

Returns a pointer to the first character of a view.

Iterators

pubbegin
cbegin

Returns an iterator/const_iterator to the beginning.

pubend
cend

Returns an iterator/const_iterator to the end.

pubrbegin
crbegin

Returns a reverse iterator/const_iterator to the beginning.

pubrend
crend

Returns a reverse iterator/const_iterator to the end.

Capacity

pubempty

Returns true if the view is empty, otherwise false.

pubsize
length

Returns the number of characters.

pubmax_size

Returns the maximum number of characters.

Modifiers

pubremove_prefix

Shrinks the view by moving its start forward.

pubremove_suffix

Shrinks the view by moving its end backward.

pubswap

Swaps the contents.

Operations

pubcopy

Copies characters.

pubsubstr

Returns a substring.

pubcompare

Compares two views.

pubstarts_with (od C++20)

Checks if the view starts with the given prefix.

pubends_with (od C++20)

Checks if the view ends with the given suffix.

pubcontains (od C++23)

Checks if the view contains the given substring or character.

pubfind

Find the first occurrence of a substring.

pubrfind

Find the last occurrence of a substring.

pubfind_first_of

Find first occurrence of characters.

pubfind_first_not_of

Find first absence of characters.

pubfind_last_of

Find last occurrence of characters.

pubfind_last_not_of

Find last absence of characters.

Constants

pubstaticconstexprnpos

A special value. The exact meaning depends on the context.

Non-member functions

puboperator==
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

puboperator<<

Performs stream output on views.

Literals

Defined in namespace std::literals::string_view_literals
puboperator ""sv

Creates a string view out of a character literal.

Helper classes

pub std::hash<std::string_view>  (od C++11)
std::hash<std::wstring_view>  (od C++11)
std::hash<std::u8string_view>  (od C++20)
std::hash<std::u16string_view>  (od C++11)
std::hash<std::u32string_view>  (od C++11)

Specializations for std::hash to support string view hashing.

Specializations

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.

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:

Examples

Basic manipulation

Creating and printing a simple string
#include <iostream>
#include <string>

int main()
{
std::string s = "World";
std::cout << "Hello, " << s << "!" << std::endl;
}
Result
Hello, World!
Concatenating strings
#include <iostream>
#include <string>

int main()
{
std::string a = "Hello, ";
std::string b = "World!";
std::string c = a + b;
std::cout << c;
}
Result
Hello, World!
Splitting a string
#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;
}
Result
Hello
World!

Conversions

Convert a string to int
#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;
}
Result
Number: 8314
Convert string to float
#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;
}
Result
Number: 3.141

Convert numbers to std::string

Convert string to float
#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;
}
Possible output
s1: 123
s2: 456.200012
s3: 3.141590
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
uwaga

Note, this article is not finished! You can help by editing this doc page.

String view class reference

Overview

template< class CharT, /* ... */ >
class basic_string;

A class that is used as a lightweight view over a sequence of characters.

Memory

important

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
pubstd::string_viewstd::basic_string_view<char>
pubstd::wstring_viewstd::basic_string_view<wchar_t>
pubstd::u8string_view (od C++20)std::basic_string_view<char8_t>
pubstd::u16string_view (od C++11)std::basic_string_view<char16_t>
pubstd::u32string_view (od 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.

Every specialization of std::basic_string_view is a TriviallyCopyable type.  (od C++11)
Named requirements

std::basic_string_view meets the requirements of:

  • TriviallyCopyable (od C++23)
    • zanotuj

      Specializations of std::basic_string_view are already trivially copyable types in all existing implementations, even before the formal requirement introduced in C++23.

Feature testing macros
important

This section requires improvement. You can help by editing this doc page.

std::string_view

Defined instring_view

Template parameters

pubCharTCharacter type
pubTraits

CharTraits class specifying the operations on the character type.

important

Like for std::basic_string, Traits::char_type must name the same type as CharT or the program is ill-formed

.

Type names

pubtraits_typeTraits
pubvalue_typeCharT
pubpointerCharT*
pubconst_pointerconst CharT*
pubreferenceCharT
pubconst_referenceconst CharT&
pubconst_iterator

implementation-defined constant LegacyInputIterator, ConstexprIterator  (od C++20) and LegacyContiguousIterator  (do C++20) contiguous_iterator  (od C++20) whose value_type is CharT

pubconst_reverse_iteratorstd::reverse_iterator<const_iterator>
pubreverse_iteratorconst_reverse_iterator
pubsize_typestd::size_t
pubdifference_typestd::ptrdiff_t
Iterators

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.

puboperator=

Assigns values to the string string view.

Element access

pubat

Accesses the specified character with bounds checking.

puboperator[]

Accesses the specified character.

pubfront

Returns the first character.

pubback

Returns the last character.

pubdata

Returns a pointer to the first character of a view.

Iterators

pubbegin
cbegin

Returns an iterator/const_iterator to the beginning.

pubend
cend

Returns an iterator/const_iterator to the end.

pubrbegin
crbegin

Returns a reverse iterator/const_iterator to the beginning.

pubrend
crend

Returns a reverse iterator/const_iterator to the end.

Capacity

pubempty

Returns true if the view is empty, otherwise false.

pubsize
length

Returns the number of characters.

pubmax_size

Returns the maximum number of characters.

Modifiers

pubremove_prefix

Shrinks the view by moving its start forward.

pubremove_suffix

Shrinks the view by moving its end backward.

pubswap

Swaps the contents.

Operations

pubcopy

Copies characters.

pubsubstr

Returns a substring.

pubcompare

Compares two views.

pubstarts_with (od C++20)

Checks if the view starts with the given prefix.

pubends_with (od C++20)

Checks if the view ends with the given suffix.

pubcontains (od C++23)

Checks if the view contains the given substring or character.

pubfind

Find the first occurrence of a substring.

pubrfind

Find the last occurrence of a substring.

pubfind_first_of

Find first occurrence of characters.

pubfind_first_not_of

Find first absence of characters.

pubfind_last_of

Find last occurrence of characters.

pubfind_last_not_of

Find last absence of characters.

Constants

pubstaticconstexprnpos

A special value. The exact meaning depends on the context.

Non-member functions

puboperator==
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

puboperator<<

Performs stream output on views.

Literals

Defined in namespace std::literals::string_view_literals
puboperator ""sv

Creates a string view out of a character literal.

Helper classes

pub std::hash<std::string_view>  (od C++11)
std::hash<std::wstring_view>  (od C++11)
std::hash<std::u8string_view>  (od C++20)
std::hash<std::u16string_view>  (od C++11)
std::hash<std::u32string_view>  (od C++11)

Specializations for std::hash to support string view hashing.

Specializations

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.

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:

Examples

Basic manipulation

Creating and printing a simple string
#include <iostream>
#include <string>

int main()
{
std::string s = "World";
std::cout << "Hello, " << s << "!" << std::endl;
}
Result
Hello, World!
Concatenating strings
#include <iostream>
#include <string>

int main()
{
std::string a = "Hello, ";
std::string b = "World!";
std::string c = a + b;
std::cout << c;
}
Result
Hello, World!
Splitting a string
#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;
}
Result
Hello
World!

Conversions

Convert a string to int
#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;
}
Result
Number: 8314
Convert string to float
#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;
}
Result
Number: 3.141

Convert numbers to std::string

Convert string to float
#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;
}
Possible output
s1: 123
s2: 456.200012
s3: 3.141590
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.