tell cin to stop reading at newline

后端 未结 6 1406
[愿得一人]
[愿得一人] 2020-11-28 11:27

Suppose I want to read line a of integers from input like this:

1 2 3 4 5\\n

I want cin to stop at \'\\n\' character but cin doesn\'t seem

6条回答
  •  甜味超标
    2020-11-28 11:56

    Use getline and istringstream:

    #include 
    /*....*/
    vector getclause() {
      char c;
      vector cl;
      std::string line;
      std::getline(cin, line);
      std::istringstream iss(line);
      while ( iss >> c) {    
        cl.push_back(c);
      }
      return cl;
    }
    

提交回复
热议问题