📄 ostream
字号:
* Tries to insert @a c. * * @note This function is not overloaded on signed char and * unsigned char. */ __ostream_type& put(char_type __c); // Core write functionality, without sentry. void _M_write(const char_type* __s, streamsize __n) { streamsize __put = this->rdbuf()->sputn(__s, __n); if (__put != __n) this->setstate(ios_base::badbit); } /** * @brief Character string insertion. * @param s The array to insert. * @param n Maximum number of characters to insert. * @return *this * * Characters are copied from @a s and inserted into the stream until * one of the following happens: * * - @a n characters are inserted * - inserting into the output sequence fails (in this case, badbit * will be set in the stream's error state) * * @note This function is not overloaded on signed char and * unsigned char. */ __ostream_type& write(const char_type* __s, streamsize __n); //@} /** * @brief Synchronizing the stream buffer. * @return *this * * If @c rdbuf() is a null pointer, changes nothing. * * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1, * sets badbit. */ __ostream_type& flush(); // [27.6.2.4] seek members /** * @brief Getting the current write position. * @return A file position object. * * If @c fail() is not false, returns @c pos_type(-1) to indicate * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out). */ pos_type tellp(); /** * @brief Changing the current write position. * @param pos A file position object. * @return *this * * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If * that function fails, sets failbit. */ __ostream_type& seekp(pos_type); /** * @brief Changing the current write position. * @param off A file offset object. * @param dir The direction in which to seek. * @return *this * * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir). * If that function fails, sets failbit. */ __ostream_type& seekp(off_type, ios_base::seekdir); protected: explicit basic_ostream() { } }; /** * @brief Performs setup work for output streams. * * Objects of this class are created before all of the standard * inserters are run. It is responsible for "exception-safe prefix and * suffix operations." Additional actions may be added by the * implementation, and we list them in * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5 * under [27.6] notes. */ template <typename _CharT, typename _Traits> class basic_ostream<_CharT, _Traits>::sentry { // Data Members: bool _M_ok; basic_ostream<_CharT,_Traits>& _M_os; public: /** * @brief The constructor performs preparatory work. * @param os The output stream to guard. * * If the stream state is good (@a os.good() is true), then if the * stream is tied to another output stream, @c is.tie()->flush() * is called to synchronize the output sequences. * * If the stream state is still good, then the sentry state becomes * true ("okay"). */ explicit sentry(basic_ostream<_CharT,_Traits>& __os); /** * @brief Possibly flushes the stream. * * If @c ios_base::unitbuf is set in @c os.flags(), and * @c std::uncaught_exception() is true, the sentry destructor calls * @c flush() on the output stream. */ ~sentry() { // XXX MT if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception()) { // Can't call flush directly or else will get into recursive lock. if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1) _M_os.setstate(ios_base::badbit); } } /** * @brief Quick status checking. * @return The sentry state. * * For ease of use, sentries may be converted to booleans. The * return value is that of the sentry state (true == okay). */ operator bool() const { return _M_ok; } }; // [27.6.2.5.4] character insertion templates //@{ /** * @brief Character inserters * @param out An output stream. * @param c A character. * @return out * * Behaves like one of the formatted arithmetic inserters described in * std::basic_ostream. After constructing a sentry object with good * status, this function inserts a single character and any required * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then * called. * * If @a c is of type @c char and the character type of the stream is not * @c char, the character is widened before insertion. */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c); template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) { return (__out << __out.widen(__c)); } // Specialization template <class _Traits> basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __out, char __c); // Signed and unsigned template<class _Traits> basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __out, signed char __c) { return (__out << static_cast<char>(__c)); } template<class _Traits> basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c) { return (__out << static_cast<char>(__c)); } //@} //@{ /** * @brief String inserters * @param out An output stream. * @param s A character string. * @return out * @pre @a s must be a non-NULL pointer * * Behaves like one of the formatted arithmetic inserters described in * std::basic_ostream. After constructing a sentry object with good * status, this function inserts @c traits::length(s) characters starting * at @a s, widened if necessary, followed by any required padding (as * determined by [22.2.2.2.2]). @c out.width(0) is then called. */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s); template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits> & operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s); // Partial specializationss template<class _Traits> basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __out, const char* __s); // Signed and unsigned template<class _Traits> basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) { return (__out << reinterpret_cast<const char*>(__s)); } template<class _Traits> basic_ostream<char, _Traits> & operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) { return (__out << reinterpret_cast<const char*>(__s)); } //@} // [27.6.2.7] standard basic_ostream manipulators /** * @brief Write a newline and flush the stream. * * This manipulator is often mistakenly used when a simple newline is * desired, leading to poor buffering performance. See * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more * on this subject. */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) { return flush(__os.put(__os.widen('\n'))); } /** * @brief Write a null character into the output sequence. * * "Null character" is @c CharT() by definition. For CharT of @c char, * this correctly writes the ASCII @c NUL character string terminator. */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) { return __os.put(_CharT()); } /** * @brief Flushes the output stream. * * This manipulator simply calls the stream's @c flush() member function. */ template<typename _CharT, typename _Traits> basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) { return __os.flush(); }} // namespace std#ifndef _GLIBCXX_EXPORT_TEMPLATE# include <bits/ostream.tcc>#endif#endif /* _GLIBCXX_OSTREAM */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -