是否可以“准备” cin的输入?

| 在他的答案中,特别是在链接的Ideone示例中,@ Nawaz显示了如何更改
cout
的缓冲区对象以写入其他内容。这使我想到了通过填充
streambuf
来利用它来准备ѭ1的输入:
#include <iostream>
#include <sstream>
using namespace std;

int main(){
        streambuf *coutbuf = cout.rdbuf(cin.rdbuf());
        cout << \"this goes to the input stream\" << endl;
        string s;
        cin >> s;
        cout.rdbuf(coutbuf);
        cout << \"after cour.rdbuf : \" << s;
        return 0;
}
但这并不能按预期工作,换句话说,它会失败。 :|
cin
仍然需要用户输入,而不是从提供的
streambuf
中读取。有没有办法使这项工作?     
已邀请:
#include <iostream>
#include <sstream>

int main()
{
    std::stringstream s(\"32 7.4\");
    std::cin.rdbuf(s.rdbuf());

    int i;
    double d;
    if (std::cin >> i >> d)
        std::cout << i << \' \' << d << \'\\n\';
}
    
忽略这个问题,在进一步调查的同时,我使它起作用。我所做的实际上是与计划相反的方式;我提供了
cin
streambuf
来读取,而不是自己填充。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(){
  stringstream ss;
  ss << \"Here be prepared input for cin\";
  streambuf* cin_buf = cin.rdbuf(ss.rdbuf());
  string s;
  while(cin >> s){
    cout << s << \" \";
  }
  cin.rdbuf(cin_buf);
}
尽管仍然很高兴看到是否有可能提供准备好的输入而不必直接更改
cin
streambuf
,也就是直接写入其缓冲区而不是从另一个缓冲区读取它。     

要回复问题请先登录注册