从键盘向两个vector vec和vec2填充字符串的程序:
#include <iostream> // For standard streams
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
static void cin_state(void)
{
cout << "[fail,eof,bad]: ["
<< cin.fail() << cin.eof() << cin.bad()
<< "], state-good: " << cin.good() << endl;
}
static void cin_clear(void)
{
cout << "\n=>before cin.clear()";
cin_state();
cin.clear();
cout << " after cin.clear()";
cin_state();
}
int main(void)
{
std::vector<std::string> vec, vec2;
std::copy(std::istream_iterator<std::string> {std::cin},
std::istream_iterator<std::string>{},
std::back_inserter(vec));
std::copy(std::cbegin(vec),
std::cend(vec),
std::ostream_iterator<std::string>{std::cout, " "});
cin_clear();
std::copy(std::istream_iterator<std::string> {std::cin},
std::istream_iterator<std::string>{},
std::back_inserter(vec2));
return 0;
}
Win10+vs2019没有问题
Linux下vec2根本就没有机会输入,好像车子挂不上档,即使我增加std::cin.ignore()也没有用:
$ Snippets ./a.out
akdf kdfj ksdj fksdfj dsf
akdf kdfj ksdj fksdfj dsf
=>before cin.clear()[fail,eof,bad]: [110], state-good: 0
after cin.clear()[fail,eof,bad]: [000], state-good: 1
$ Snippets
--
修改:ActiveIon FROM 61.149.156.*
FROM 61.149.156.*