C++ basic_ios 库 - sync
描述
它用于同步输入缓冲区。
声明
以下是 std::basic_istream::sync 的声明。
int sync();
参数
none
返回值
如果函数失败,或者因为没有流缓冲区对象与流关联(rdbuf 为空),或者因为对其 pubsync 成员的调用失败,它返回 -1。否则,它返回零,表示成功。
异常
Basic guarantee − 如果抛出异常,则对象处于有效状态。
数据竞争
修改流对象。
示例
在下面的 std::basic_istream::sync 示例中。
#include <iostream>
int main () {
char first, second;
std::cout << "Please, enter a word: ";
first = std::cin.get();
std::cin.sync();
std::cout << "Please, enter another word: ";
second = std::cin.get();
std::cout << "The first word began by " << first << '\n';
std::cout << "The second word began by " << second << '\n';
return 0;
}
输出应该是这样的 −
Please, enter a word: test Please enter another word: text The first word began by t The second word began by t

