put
描述 (Description)
它用于将字符c插入流中。该函数通过首先构造一个sentry对象来访问输出序列。 然后(如果好),它将c插入其关联的流缓冲区对象,就像调用其成员函数sputc一样,最后在返回之前销毁sentry对象。
声明 (Declaration)
以下是std :: ostream :: put的声明。
ostream& put (char c);
参数 (Parameters)
c - 要写的字符。
返回值 (Return Value)
它返回ostream对象(* this)。
异常 (Exceptions)
Basic guarantee - 如果抛出异常,则对象处于有效状态。
数据竞争 (Data races)
修改流对象。 当与stdio同步时,对标准流对象(cout,cerr,clog)除外,对同一流对象的并发访问可能会导致数据争用。
例子 (Example)
在下面的例子中解释了std :: ostream :: put。
#include <iostream>
#include <fstream>
int main () {
std::ofstream outfile ("test.txt");
char ch;
std::cout << "Type some text (type a dot to finish):\n";
do {
ch = std::cin.get();
outfile.put(ch);
} while (ch!='.');
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Type some text (type a dot to finish):
iowiki.