Redirect cin to a string

前端 未结 4 676
长发绾君心
长发绾君心 2020-12-16 19:14

I want to have cin read input from a string.

Is there a way to have it do this?

Something like this:

const char * s = \"123 ab\";
cin.readFro         


        
4条回答
  •  粉色の甜心
    2020-12-16 20:00

    In C++17, Ben Voigt's solution won't compile unless you use basic_stringbuf. Instead use the one below:

      stringbuf s;
      const char *userInput = "10 1 2 3 4 5 6 7 8 9 10 3 7";         
      s.sputn(userInput, strlen(userInput)); 
      cin.rdbuf(&s);
    

提交回复
热议问题