Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::pop_back

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      void pop_back();
      (constexpr since C++20)

      Removes the last character from the string.

      Equivalent toerase(end()-1).

      Ifempty() istrue, the behavior is undefined.

      (until C++26)

      Ifempty() 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]Complexity

      Constant.

      [edit]Exceptions

      Throws nothing.

      [edit]Notes

      In libstdc++,pop_back() isnot available in C++98 mode.

      [edit]Example

      Run this code
      #include <cassert>#include <iomanip>#include <iostream>#include <string> int main(){std::string str("Short string!");std::cout<<"Before: "<<std::quoted(str)<<'\n';assert(str.size()==13);     str.pop_back();std::cout<<"After:  "<<std::quoted(str)<<'\n';assert(str.size()==12);     str.clear();//  str.pop_back(); // undefined behavior}

      Output:

      Before: "Short string!"After:  "Short string"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 534C++98std::basic_string did not have the member functionpop_back()added

      [edit]See also

      appends a character to the end
      (public member function)[edit]
      removes characters
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/pop_back&oldid=182303"

      [8]ページ先頭

      ©2009-2025 Movatter.jp