Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_streambuf<CharT,Traits>::sputbackc

      From cppreference.com
      <cpp‎ |io‎ |basic streambuf
       
       
       
      std::basic_streambuf
       
      int_type sputbackc( char_type c);

      Puts back a character back to the get area.

      If a putback position is available in the get area (gptr()> eback()), and the characterc is equal to the character one position to the left ofgptr() (as determined byTraits::eq(c, gptr()[-1]), then simply decrements the next pointer (gptr()).

      Otherwise, callspbackfail(Traits::to_int_type(c)) to either back up the get area or to modify both the get area and possibly the associated character sequence.

      The I/O stream functionbasic_istream::putback is implemented in terms of this function.

      Contents

      [edit]Parameters

      c - character to put back

      [edit]Return value

      If putback position was available, returns the character that the next pointer is now pointing at, converted toint_type withTraits::to_int_type(*gptr()). The next single-character input from this streambuf will return this character.

      If putback position was not available, returns whatpbackfail() returns, which isTraits::eof() on failure.

      [edit]Example

      Run this code
      #include <iostream>#include <sstream> int main(){std::stringstream s("abcdef");// gptr() points to 'a' in "abcdef"std::cout<<"Before putback, string holds "<< s.str()<<'\n';char c1= s.get();// c1 = 'a', gptr() now points to 'b' in "abcdef"char c2= s.rdbuf()->sputbackc('z');// same as s.putback('z')// gptr() now points to 'z' in "zbcdef"std::cout<<"After putback, string holds "<< s.str()<<'\n';char c3= s.get();// c3 = 'z', gptr() now points to 'b' in "zbcdef"char c4= s.get();// c4 = 'b', gptr() now points to 'c' in "zbcdef"std::cout<< c1<< c2<< c3<< c4<<'\n';     s.rdbuf()->sputbackc('b');// gptr() now points to 'b' in "zbcdef"    s.rdbuf()->sputbackc('z');// gptr() now points to 'z' in "zbcdef"int eof= s.rdbuf()->sputbackc('x');// nothing to unget: pbackfail() failsif(eof==EOF)std::cout<<"No room to putback after 'z'\n";}

      Output:

      Before putback, string holds abcdefAfter putback, string holds zbcdefazzbNo room to putback after 'z'

      [edit]See also

      moves the next pointer in the input sequence back by one
      (public member function)[edit]
      puts a character into input stream
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_streambuf/sputbackc&oldid=105209"

      [8]ページ先頭

      ©2009-2025 Movatter.jp