清空输入缓冲区:
fflush(stdin);
std::cin.sync();
清空输出缓冲区:
fflush(stdout);
std::cout.flush();
endl也有清空输出缓冲区的功能.
int main(int argc, char *argv[]) {
std::string str1, str2;
std::cin >> str1;
std::cin.sync();
std::cin >> str2;
std::cout << str1 << std::endl << str2 << std::endl;
//fflush(stdin);
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
int v = 0;
while (std::cin >> v) {
std::cout << v << std::endl;
}
// The order clear() and sync() is very important.
// Clear the input stream's state
// and then clear the input stream's content
// to input new string.
std::cin.clear();
std::cin.sync();
std::string str;
std::cin >> str;
std::cout << str << std::endl;
std::cout << v << std::endl;
return EXIT_SUCCESS;
}