그것은 가능한"준비"하는 입력 cin?
문제
에 그의 대답이, 특별히, 연 Ideone 예,@Nawaz 할 수 있는 방법을 보여줍 버퍼를 변경의 목 cout
를 작성하는 다른 뭔가가 있습니다.이에 대해 생각해 보게 되었을 활용하는 것을 준비하는 입력 cin
, 충전하여,그것의 streambuf
:
#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
a 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
바로,일명 쓰기를 버퍼에 대신 직접 그것을 읽고 다른 하나입니다.
제휴하지 않습니다 StackOverflow