basic_ostream 클래스

이 클래스 템플릿은 traits_type라고도 하는 클래스에 의해 문자 특성이 결정되는 char_type 형식의 Elem요소를 사용하여 스트림 버퍼에 요소 및 인코딩된 Tr개체의 삽입을 제어하는 개체를 설명합니다.

구문

template <class Elem, class Tr = char_traits<Elem>>
class basic_ostream : virtual public basic_ios<Elem, Tr>

매개 변수

Elem
char_type.

Tr
문자 traits_type입니다.

설명

오버로드 연산<<자를 오버로드하는 멤버 함수의 대부분은 형식이 지정된 출력 함수입니다. 다음 패턴을 따릅니다.

iostate state = goodbit;
const sentry ok(*this);

if (ok)
{try
{<convert and insert elements
    accumulate flags in state> }
    catch (...)
{try
{setstate(badbit);

}
    catch (...)
{}
    if ((exceptions()& badbit) != 0)
    throw; }}
width(0);
// Except for operator<<(Elem)
setstate(state);

return (*this);

다른 두 멤버 함수는 형식이 지정되지 않은 출력 함수입니다. 다음 패턴을 따릅니다.

iostate state = goodbit;
const sentry ok(*this);

if (!ok)
    state |= badbit;
else
{try
{<obtain and insert elements
    accumulate flags in state> }
    catch (...)
{try
{setstate(badbit);

}
    catch (...)
{}
    if ((exceptions()& badbit) != 0)
    throw; }}
setstate(state);

return (*this);

두 함수 그룹은 요소를 삽입하는 동안 오류가 발생하면 setstate(badbit)를 호출합니다.

Elem을 basic_istream<클래스의 개체인 Tr>은 클래스 basic_ios<Elem, Tr>가상 공용 기본 개체만 저장합니다.

예시

출력 스트림에 대한 자세한 내용은 basic_ofstream 클래스에 대한 예제를 참조하세요.

생성자

생성자 Description
basic_ostream basic_ostream 개체를 생성합니다.

멤버 함수

멤버 함수 설명
flush 버퍼를 플러시합니다.
put 스트림에 문자를 넣습니다.
seekp 출력 스트림 내의 위치를 다시 설정합니다.
sentry 중첩 클래스는 선언에서 형식이 지정된 출력 함수 및 형식이 지정되지 않은 출력 함수를 구성하는 개체를 설명합니다.
swap basic_ostream 개체의 값을 제공된 basic_ostream 개체의 값으로 교환합니다.
tellp 출력 스트림 내의 위치를 보고합니다.
write 스트림에 문자를 넣습니다.

연산자

연산자 설명
operator= 제공된 basic_ostream 개체 매개 변수의 값을 이 개체에 할당합니다.
연산자<< 스트림에 씁니다.

요구 사항

헤더:<ostream>

네임스페이스: std

basic_ostream::basic_ostream

basic_ostream 개체를 생성합니다.

explicit basic_ostream(
    basic_streambuf<Elem, Tr>* strbuf,
    bool _Isstd = false);

basic_ostream(basic_ostream&& right);

매개 변수

strbuf
basic_streambuf 형식의 개체입니다.

_Isstd
표준 스트림인 경우 true, 아닌 경우 false입니다.

right
basic_ostream 형식의 개체에 대한 rvalue 참조입니다.

설명

첫 번째 생성자는 init(strbuf)를 호출하여 기본 개체를 초기화합니다. 두 번째 생성자는 basic_ios::move(right)를 호출하여 기본 개체를 초기화합니다.

예시

출력 스트림에 대한 자세한 내용은 basic_ofstream::basic_ofstream에 대한 예제를 참조하세요.

basic_ostream::flush

버퍼를 플러시합니다.

basic_ostream<Elem, Tr>& flush();

Return Value

Basic_ostream 개체에 대한 참조입니다.

설명

rdbuf가 null 포인터가 아닌 경우 함수는 rdbuf-pubsync>를 호출합니다. -1이 반환되는 경우 함수는 setstate(badbit)를 호출합니다. *this를 반환합니다.

예시

// basic_ostream_flush.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
   using namespace std;
   cout << "test";
   cout.flush();
}
test

basic_ostream::operator<<

스트림에 씁니다.

basic_ostream<Elem, Tr>& operator<<(
    basic_ostream<Elem, Tr>& (* Pfn)(basic_ostream<Elem, Tr>&));

basic_ostream<Elem, Tr>& operator<<(
    ios_base& (* Pfn)(ios_base&));

basic_ostream<Elem, Tr>& operator<<(
    basic_ios<Elem, Tr>& (* Pfn)(basic_ios<Elem, Tr>&));

basic_ostream<Elem, Tr>& operator<<(basic_streambuf<Elem, Tr>* strbuf);
basic_ostream<Elem, Tr>& operator<<(bool val);
basic_ostream<Elem, Tr>& operator<<(short val);
basic_ostream<Elem, Tr>& operator<<(unsigned short val);
basic_ostream<Elem, Tr>& operator<<(int __w64  val);
basic_ostream<Elem, Tr>& operator<<(unsigned int __w64  val);
basic_ostream<Elem, Tr>& operator<<(long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long __w64  val);
basic_ostream<Elem, Tr>& operator<<(long long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long long val);
basic_ostream<Elem, Tr>& operator<<(float val);
basic_ostream<Elem, Tr>& operator<<(double val);
basic_ostream<Elem, Tr>& operator<<(long double val);
basic_ostream<Elem, Tr>& operator<<(const void* val);

매개 변수

Pfn
함수 포인터입니다.

strbuf
stream_buf 개체에 대한 포인터입니다.

val
스트림에 기록할 요소입니다.

Return Value

Basic_ostream 개체에 대한 참조입니다.

설명

또한 ostream> 헤더는 <여러 전역 삽입 연산자를 정의합니다. 자세한 내용은 operator<<를 참조하세요.

첫 번째 멤버 함수는 폼 ostr << endl 의 식이 endl(ostr)을 호출한 다음 *this를 반환하도록 합니다. 두 번째와 세 번째 함수는 hex와 같은 기타 조작자가 비슷하게 동작하도록 합니다. 나머지 함수는 모두 형식이 지정된 출력 함수입니다.

함수

basic_ostream<Elem, Tr>& operator<<(basic_streambuf<Elem, Tr>* strbuf);

strbuf가 null 포인터가 아닌 경우 strbuf에서 요소를 추출하고 삽입합니다. 추출은 파일의 끝에서 또는 추출이 예외를 throw하는 경우(다시 throw됨) 중지됩니다. 또한 삽입이 실패하면 문제의 요소를 추출하지 않은 채 중단됩니다. 함수가 요소를 삽입하지 않거나 추출이 예외를 throw하면 함수는 setstate(failbit)를 호출합니다. 어쨌든 함수는 *this를 반환합니다.

함수

basic_ostream<Elem, Tr>& operator<<(bool val);

_Val 는 부울 필드로 변환하고 use_facet num_put<<Elem, OutIt>(getloc)를 호출하여 삽입합니다. put(OutIt(rdbuf), *this, getloc, val). OutIt 여기서는 ostreambuf_iterator<Elem, Tr>로 정의됩니다. 함수는 *this를 반환합니다.

함수

basic_ostream<Elem, Tr>& operator<<(short val);
basic_ostream<Elem, Tr>& operator<<(unsigned short val);
basic_ostream<Elem, Tr>& operator<<(int val);
basic_ostream<Elem, Tr>& operator<<(unsigned int __w64  val);
basic_ostream<Elem, Tr>& operator<<(long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long val);
basic_ostream<Elem, Tr>& operator<<(long long val);
basic_ostream<Elem, Tr>& operator<<(unsigned long long val);
basic_ostream<Elem, Tr>& operator<<(const void* val);

각각 val을 숫자 필드로 변환하고 use_facet num_put<<Elem, OutIt>(getloc)를 호출하여 삽입합니다. put(OutIt(rdbuf), *this, getloc, val). 여기서 OutIt는 ostreambuf_iterator<Elem, Tr>정의됩니다. 함수는 *this를 반환합니다.

함수

basic_ostream<Elem, Tr>& operator<<(float val);
basic_ostream<Elem, Tr>& operator<<(double val);
basic_ostream<Elem, Tr>& operator<<(long double val);

각각 val을 숫자 필드로 변환하고 use_facet num_put<<Elem, OutIt>(getloc)를 호출하여 삽입합니다. put(OutIt(rdbuf), *this, getlocval). 여기서 OutIt는 ostreambuf_iterator<Elem, Tr>정의됩니다. 함수는 *this를 반환합니다.

예시

// basic_ostream_op_write.cpp
// compile with: /EHsc
#include <iostream>
#include <string.h>

using namespace std;

ios_base& hex2( ios_base& ib )
{
   ib.unsetf( ios_base::dec );
   ib.setf( ios_base::hex );
   return ib;
}

basic_ostream<char, char_traits<char> >& somefunc(basic_ostream<char, char_traits<char> > &i)
{
    if (i == cout)
    {
        i << "i is cout" << endl;
    }
    return i;
}

class CTxtStreambuf : public basic_streambuf< char, char_traits< char > >
{
public:
    CTxtStreambuf(char *_pszText)
    {
        pszText = _pszText;
        setg(pszText, pszText, pszText + strlen(pszText));
    };
    char *pszText;
};

int main()
{
    cout << somefunc;
    cout << 21 << endl;

    hex2(cout);
    cout << 21 << endl;

    CTxtStreambuf f("text in streambuf");
    cout << &f << endl;
}

basic_ostream::operator=

제공된 basic_ostream 개체 매개 변수의 값을 이 개체에 할당합니다.

basic_ostream& operator=(basic_ostream&& right);

매개 변수

right
basic_ostream 개체에 대한 rvalue 참조입니다.

설명

멤버 연산자는 swap (right)를 호출합니다.

basic_ostream::p ut

스트림에 문자를 넣습니다.

basic_ostream<Elem, Tr>& put(char_type _Ch);

매개 변수

_채널
단일 문자입니다.

Return Value

Basic_ostream 개체에 대한 참조입니다.

설명

서식이 지정되지 않은 출력 함수는 요소 _Ch 삽입합니다. *this를 반환합니다.

예시

// basic_ostream_put.cpp
// compile with: /EHsc
#include <iostream>

int main( )
{
   using namespace std;
   cout.put( 'v' );
   cout << endl;
   wcout.put( L'l' );
}
v
l

basic_ostream::seekp

출력 스트림 내의 위치를 다시 설정합니다.

basic_ostream<Elem, Tr>& seekp(pos_type _Pos);

basic_ostream<Elem, Tr>& seekp(off_type _Off, ios_base::seekdir _Way);

매개 변수

_Pos
스트림 내의 위치입니다.

_Off
_Way 상대적인 오프셋입니다.

_방법
ios_base::seekdir 열거형 중 하나입니다.

Return Value

Basic_ostream 개체에 대한 참조입니다.

설명

fail이면 첫 번째 멤버 함수는 false일부 pos_type 임시 개체에 대해 newpos =rdbuf-pubseekpos>(_Pos)를 호출합니다.newpos false이면 fail 두 번째 함수는 newpos = rdbuf-pubseekoff>(_Off, _Way)를 호출합니다. 어떤 경우든 (off_type)newpos == (off_type)(-1)(배치 작업 실패)이면 함수는 istr.setstate(failbit)를 호출합니다. 두 함수 모두 *this를 반환합니다.

예시

// basic_ostream_seekp.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>

int main()
{
    using namespace std;
    ofstream x("basic_ostream_seekp.txt");
    streamoff i = x.tellp();
    cout << i << endl;
    x << "testing";
    i = x.tellp();
    cout << i << endl;
    x.seekp(2);   // Put char in third char position in file
    x << " ";

    x.seekp(2, ios::end);   // Put char two after end of file
    x << "z";
}
0
7

basic_ostream::sentry

중첩 클래스는 선언에서 형식이 지정된 출력 함수 및 형식이 지정되지 않은 출력 함수를 구성하는 개체를 설명합니다.

class sentry { public: explicit sentry(basic_ostream<Elem, Tr>& _Ostr); operator bool() const; ~sentry(); };

설명

중첩 클래스는 선언에서 형식이 지정된 출력 함수 및 형식이 지정되지 않은 출력 함수를 구성하는 개체를 설명합니다. ostr인 경우 good is true and ostr.tie는 null 포인터가 아니며 생성자는 ostr.tie-flush를> 호출합니다. 그런 다음 생성자는 반환된 값을 ostr.good 저장합니다 status. 이 저장된 값을 전달하기 위한 operator bool 이후 호출입니다.

반환 falseflags&unitbuf가 0이 아니면 uncaught_exception 소멸자가 플러시를 호출합니다.

basic_ostream::swap

basic_ostream 개체의 값을 제공된 basic_ostream의 값으로 교환합니다.

void swap(basic_ostream& right);

매개 변수

right
basic_ostream 개체에 대한 참조입니다.

설명

멤버 함수는 basic_ios::swap(right)을 호출하여 이 개체의 내용을 오른쪽 내용으로 교환합니다.

basic_ostream::tellp

출력 스트림 내의 위치를 보고합니다.

pos_type tellp();

Return Value

출력 스트림 내의 위치입니다.

설명

fail이면 멤버 함수는 falserdbuf-pubseekoff>(0, curin)를 반환합니다. 아닌 경우 pos_type(-1)를 반환합니다.

예시

tellp 사용 예제는 seekp를 참조하세요.

basic_ostream::write

스트림에 문자를 넣습니다.

basic_ostream<Elem, Tr>& write(const char_type* str, streamsize count);

매개 변수

count
스트림에 넣을 문자의 수입니다.

str
스트림에 넣을 문자입니다.

Return Value

Basic_ostream 개체에 대한 참조입니다.

설명

형식이 지정되지 않은 출력 함수는 str에서 시작하는 개수 요소의 시퀀스를 삽입합니다.

예시

write 사용 예제는 streamsize를 참조하세요.

참고 항목

C++ 표준 라이브러리의 스레드 보안
iostream 프로그래밍
iostreams 규칙