Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string_view<CharT,Traits>::basic_string_view

      From cppreference.com
      <cpp‎ |string‎ |basic string view


       
       
       
      std::basic_string_view
       
      constexpr basic_string_view()noexcept;
      (1)(since C++17)
      constexpr basic_string_view(const basic_string_view& other)noexcept=default;
      (2)(since C++17)
      constexpr basic_string_view(const CharT* s, size_type count);
      (3)(since C++17)
      constexpr basic_string_view(const CharT* s);
      (4)(since C++17)
      template<class It,class End>
      constexpr basic_string_view( It first, End last);
      (5)(since C++20)
      template<class R>
      constexprexplicit basic_string_view( R&& r);
      (6)(since C++23)
      basic_string_view(std::nullptr_t)= delete;
      (7)(since C++23)
      1) Default constructor. Constructs an emptystd::basic_string_view. After construction,data() is equal tonullptr, andsize() is equal to0.
      2) Copy constructor. Constructs a view of the same content asother. After construction,data() is equal toother.data(), andsize() is equal toother.size().
      3) Constructs a view of the firstcount characters of the character array starting with the element pointed bys.s can contain null characters. The behavior is undefined if[ss+ count) is not a valid range (even though the constructor may not access any of the elements of this range). After construction,data() is equal tos, andsize() is equal tocount.
      4) Constructs a view of the null-terminated character string pointed to bys, not including the terminating null character. The length of the view is determined as if byTraits::length(s). The behavior is undefined if[ss+ Traits::length(s)) is not a valid range. After construction,data() is equal tos, andsize() is equal toTraits::length(s).
      5) Constructs astd::basic_string_view over the range[firstlast). The behavior is undefined if[firstlast) is not a valid range, ifIt does not actually modelcontiguous_iterator, or ifEnd does not actually modelsized_sentinel_for forIt. After construction,data() is equal tostd::to_address(first), andsize() is equal tolast- first.

      This overload participates in overload resolution only if all following conditions are satisfied:

      6) Constructs astd::basic_string_view over the ranger. After construction,data() is equal toranges::data(r), andsize() is equal toranges::size(r).

      This overload participates in overload resolution only if all following conditions are satisfied:

      7)std::basic_string_view cannot be constructed fromnullptr.

      Contents

      [edit]Parameters

      other - another view to initialize the view with
      s - pointer to a character array or a C string to initialize the view with
      count - number of characters to include in the view
      first - iterator to the first character of the sequence
      last - iterator past the last character of the sequence or another sentinel
      r - a contiguous range that contains the sequence

      [edit]Complexity

      1-3,5,6) Constant.
      4) Linear in length ofs.

      [edit]Example

      Run this code
      #include <array>#include <iomanip>#include <iostream>#include <string>#include <string_view> int main(){std::string cppstr="Foo";std::string_view cppstr_v(cppstr);// overload (2), after// std::string::operator string_viewstd::cout<<"1) cppstr_v: "<<std::quoted(cppstr_v)<<'\n'; char array[3]={'B','a','r'};std::string_view array_v(array,std::size(array));// overload (3)std::cout<<"2) array_v: "<<std::quoted(array_v)<<'\n'; constchar* one_0_two="One\0Two"; std::string_view one_two_v{one_0_two,7};// overload (3)std::cout<<"3) one_two_v:\"";for(char c: one_two_v)std::cout<<(c!='\0'? c:'?');std::cout<<"\", one_two_v.size(): "<< one_two_v.size()<<'\n'; std::string_view one_v{one_0_two};// overload (4)std::cout<<"4) one_v: "<<std::quoted(one_v)<<", one_v.size(): "<< one_v.size()<<'\n'; constexprstd::wstring_view wcstr_v= L"xyzzy";// overload (4)std::cout<<"5) wcstr_v.size(): "<< wcstr_v.size()<<'\n'; std::array ar={'P','u','b'};std::string_view ar_v(ar.begin(), ar.end());// overload (5), C++20std::cout<<"6) ar_v: "<<std::quoted(ar_v)<<'\n'; //  std::string_view ar_v2{ar}; // overload (6), OK in C++23//  std::cout << "ar_v2: " << std::quoted(ar_v2) << '\n'; // ar_v2: "Pub" [[maybe_unused]]auto zero=[]{/* ... */return nullptr;};//  std::string_view s{zero()}; // overload (7), won't compile since C++23}

      Output:

      1) cppstr_v: "Foo"2) array_v: "Bar"3) one_two_v: "One?Two", one_two_v.size(): 74) one_v: "One", one_v.size(): 35) wcstr_v.size(): 56) ar_v: "Pub"

      [edit]See also

      assigns a view
      (public member function)[edit]
      constructs abasic_string
      (public member function ofstd::basic_string<CharT,Traits,Allocator>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string_view/basic_string_view&oldid=181438"

      [8]ページ先頭

      ©2009-2025 Movatter.jp