Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |string‎ |basic string view
       
       
       
      std::basic_string_view
       
      constexpr basic_string_view substr( size_type pos=0,
                                          size_type count= npos)const;
      (since C++17)

      Returns a view of the substring[pospos+ rlen), whererlen is the smaller ofcount andsize()- pos.

      Contents

      [edit]Parameters

      pos - position of the first character
      count - requested length

      [edit]Return value

      View of the substring[pospos+ rlen).

      [edit]Exceptions

      std::out_of_range ifpos> size().

      [edit]Complexity

      Constant.

      [edit]Example

      Run this code
      #include <cstddef>#include <iostream>#include <stdexcept>#include <string_view> int main(){typedefstd::size_t count_t, pos_t; constexprstd::string_view data{"ABCDEF"}; std::cout<< data.substr()<<'\n';// ABCDEF, i.e. data[0, 5] that is [0, 6)std::cout<< data.substr(pos_t(1))<<'\n';// BCDEF, i.e. [1, 6)std::cout<< data.substr(pos_t(2), count_t(3))<<'\n';// CDE, i.e. [2, 2 + 3)std::cout<< data.substr(pos_t(4), count_t(42))<<'\n';// EF, i.e. [4, 6) try{[[maybe_unused]]auto sub= data.substr(pos_t(666), count_t(1));// throws: pos > size()}catch(std::out_of_rangeconst& ex){std::cout<< ex.what()<<'\n';}}

      Possible output:

      ABCDEFBCDEFCDEEFbasic_string_view::substr: __pos (which is 666) > __size (which is 6)

      [edit]See also

      copies characters
      (public member function)[edit]
      find characters in the view
      (public member function)[edit]
      returns a substring
      (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/substr&oldid=156778"

      [8]ページ先頭

      ©2009-2025 Movatter.jp