Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::operator[]

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      CharT& operator[]( size_type pos);
      (1)(constexpr since C++20)
      const CharT& operator[]( size_type pos)const;
      (2)(constexpr since C++20)

      Returns a reference to the character at specified locationpos ifpos< size(), or ifpos== size():

      1)

      The behavior is undefined.

      (until C++11)

      Returns a reference toCharT(), if the object referred by the returned reference is modified to any value other thanCharT(), the behavior is undefined.

      (since C++11)
      2) Returns a reference toCharT().

      Ifpos> size() istrue, the behavior is undefined.

      (until C++26)

      Ifpos> size() istrue:

      • If the implementation ishardened, acontract violation occurs. Moreover, if the contract-violation handler returns under “observe” evaluation semantic, the behavior is undefined.
      • If the implementation is not hardened, the behavior is undefined.
      (since C++26)

      Contents

      [edit]Parameters

      pos - position of the character to return

      [edit]Return value

      1)*(begin()+ pos) ifpos< size(), or a reference toCharT() ifpos== size()(since C++11).
      2)*(begin()+ pos) ifpos< size(), or a reference toCharT() ifpos== size().

      [edit]Complexity

      Constant.

      [edit]Example

      Run this code
      #include <iostream>#include <string> int main(){conststd::string e("Exemplar");for(unsigned i= e.length()-1; i!=0; i/=2)std::cout<< e[i];std::cout<<'\n'; constchar* c=&e[0];std::cout<< c<<'\n';// print as a C string // Change the last character of s into a 'y'std::string s("Exemplar ");    s[s.size()-1]='y';// equivalent to s.back() = 'y';std::cout<< s<<'\n';}

      Output:

      rmxExemplarExemplary

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 259C++98overload(1) could return const lvalue
      data()[pos], which was ill-formed
      changed to return
      *(begin()+ pos)
      LWG 2475C++11ifpos== size(), the behavior of modifying the
      object referred by the returned reference was undefined
      well-defined if
      modified toCharT()

      [edit]See also

      accesses the specified character with bounds checking
      (public member function)[edit]
      (DR*)
      accesses the first character
      (public member function)[edit]
      (DR*)
      accesses the last character
      (public member function)[edit]
      accesses the specified character
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator_at&oldid=182355"

      [8]ページ先頭

      ©2009-2025 Movatter.jp