Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string_view

      From cppreference.com
      <cpp‎ |string
       
       
      Strings library
      Classes
      basic_string_view
      (C++17)
       
      std::basic_string_view
       
      Defined in header<string_view>
      template<

         class CharT,
         class Traits=std::char_traits<CharT>

      >class basic_string_view;
      (since C++17)

      The class templatebasic_string_view describes an object that can refer to a constant contiguous sequence ofCharT with the first element of the sequence at position zero.

      For abasic_string_viewstr, pointers, iterators, and references to elements ofstr are invalidated when an operation invalidates a pointer in the range[str.data()str.data()+ str.size()).

      Every specialization ofstd::basic_string_view is aTriviallyCopyable type.

      (since C++23)

      Several typedefs for common character types are provided:

      Defined in header<string_view>
      Type Definition
      std::string_view(C++17)std::basic_string_view<char>
      std::wstring_view(C++17)std::basic_string_view<wchar_t>
      std::u8string_view(C++20)std::basic_string_view<char8_t>
      std::u16string_view(C++17)std::basic_string_view<char16_t>
      std::u32string_view(C++17)std::basic_string_view<char32_t>

      Contents

      [edit]Template parameters

      CharT - character type
      Traits -CharTraits class specifying the operations on the character type. Like forstd::basic_string,Traits::char_type must name the same type asCharT or the program is ill-formed.

      [edit]Nested types

      Type Definition
      traits_typeTraits
      value_typeCharT
      pointerCharT*
      const_pointerconst CharT*
      referenceCharT&
      const_referenceconst CharT&
      const_iterator implementation-defined constantLegacyRandomAccessIterator,
      andLegacyContiguousIterator(until C++20)
      ConstexprIterator, andcontiguous_iterator(since C++20)

      whosevalue_type isCharT

      iteratorconst_iterator
      const_reverse_iteratorstd::reverse_iterator<const_iterator>
      reverse_iteratorconst_reverse_iterator
      size_typestd::size_t
      difference_typestd::ptrdiff_t

      Note:iterator andconst_iterator are the same type because string views are views into constant character sequences.

      All requirements on the iterator types of aContainer applies to theiterator andconst_iterator types ofbasic_string_view as well.

      [edit]Data members

      Member Description
      const_pointerdata_ a pointer to the underlying sequence
      (exposition-only member object*)
      size_typesize_ the number of characters
      (exposition-only member object*)

      [edit]Member functions

      Constructors and assignment
      constructs abasic_string_view
      (public member function)[edit]
      assigns a view
      (public member function)[edit]
      Iterators
      returns an iterator to the beginning
      (public member function)[edit]
      returns an iterator to the end
      (public member function)[edit]
      returns a reverse iterator to the beginning
      (public member function)[edit]
      returns a reverse iterator to the end
      (public member function)[edit]
      Element access
      accesses the specified character
      (public member function)[edit]
      accesses the specified character with bounds checking
      (public member function)[edit]
      accesses the first character
      (public member function)[edit]
      accesses the last character
      (public member function)[edit]
      returns a pointer to the first character of a view
      (public member function)[edit]
      Capacity
      returns the number of characters
      (public member function)[edit]
      returns the maximum number of characters
      (public member function)[edit]
      checks whether the view is empty
      (public member function)[edit]
      Modifiers
      shrinks the view by moving its start forward
      (public member function)[edit]
      shrinks the view by moving its end backward
      (public member function)[edit]
      swaps the contents
      (public member function)[edit]
      Operations
      copies characters
      (public member function)[edit]
      returns a substring
      (public member function)[edit]
      compares two views
      (public member function)[edit]
      checks if the string view starts with the given prefix
      (public member function)[edit]
      (C++20)
      checks if the string view ends with the given suffix
      (public member function)[edit]
      (C++23)
      checks if the string view contains the given substring or character
      (public member function)[edit]
      find characters in the view
      (public member function)[edit]
      find the last occurrence of a substring
      (public member function)[edit]
      find first occurrence of characters
      (public member function)[edit]
      find last occurrence of characters
      (public member function)[edit]
      find first absence of characters
      (public member function)[edit]
      find last absence of characters
      (public member function)[edit]

      Constants

      [static]
      special value. The exact meaning depends on the context
      (public static member constant)[edit]

      [edit]Non-member functions

      (C++17)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
      lexicographically compares two string views
      (function template)[edit]
      Input/output
      (C++17)
      performs stream output on string views
      (function template)[edit]

      [edit]Literals

      Defined in inline namespacestd::literals::string_view_literals
      creates a string view of a character array literal
      (function)[edit]

      [edit]Helper classes

      hash support for string views
      (class template specialization)[edit]

      [edit]Helper templates

      template<class CharT,class Traits>

      inlineconstexprbool

         ranges::enable_borrowed_range<std::basic_string_view<CharT, Traits>>=true;
      (since C++20)

      This specialization ofranges::enable_borrowed_range makesbasic_string_view satisfyborrowed_range.

      template<class CharT,class Traits>

      inlineconstexprbool

         ranges::enable_view<std::basic_string_view<CharT, Traits>>=true;
      (since C++20)

      This specialization ofranges::enable_view makesbasic_string_view satisfyview.

      Deduction guides

      (since C++20)

      [edit]Notes

      It is the programmer's responsibility to ensure thatstd::string_view does not outlive the pointed-to character array:

      std::string_view good{"a string literal"};// "Good" case: `good` points to a static array.// String literals reside in persistent data storage. std::string_view bad{"a temporary string"s};// "Bad" case: `bad` holds a dangling pointer since the std::string temporary,// created by std::operator""s, will be destroyed at the end of the statement.

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

      Feature-test macroValueStdFeature
      __cpp_lib_string_view201606L(C++17)std::string_view
      201803L(C++20)ConstexprIterator
      __cpp_lib_string_contains202011L(C++23)contains

      [edit]Example

      Run this code
      #include <iostream>#include <string_view> int main(){#define A "▀"#define B "▄"#define C "─" constexpr std::string_view blocks[]{A B C, B A C, A C B, B C A}; for(int y{}, p{}; y!=8;++y, p=((p+1)%4)){for(char x{}; x!=29;++x)std::cout<< blocks[p];std::cout<<'\n';}}

      Output:

      ▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▀─▄▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀▄─▀

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3203C++17only pointers, iterators, and references
      returned from the member functions of
      basic_string_view might be invalidated
      all pointers, iterators, and references
      to elements ofbasic_string_view
      may be invalidated

      [edit]See also

      stores and manipulates sequences of characters
      (class template)[edit]
      concatenates two strings, a string and achar, or a string andstring_view
      (function template)[edit]
      (C++20)
      a non-owning view over a contiguous sequence of objects
      (class template)[edit]
      references a temporary array created inlist-initialization
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string_view&oldid=182322"

      [8]ページ先頭

      ©2009-2025 Movatter.jp