IO 特性

IO特性

输入流特性

  • 根据空格来读取和分割数据,其中空格的个数不影响数据的读取: cin >> n >> f >> s;

    • 例如: 10 4.5 abd 和10 4.5 abd没有区别
  • 按行读取数据:

    • istream& getline (istream& is, string &str, char delim=' ');

    • 满足如下条件读取完成:

      • 到达了文件结尾
      • 读取了字符串的最大字符数
      • 读取了分割字符
    • 例子

      string names[100];
      int i = 0;
      while ( getline( cin, names[i] ) ) {
      i++;
      }
      

输出特性

  • cout

    • int age = 21;
      string name = “John Doe”;
      cout << name << “ is ” << age << “ years old.” << endl;
      

完整代码

#include <iostream>
#include <sstream> // defines the stringstream class
using namespace std;

int main()
{
    int age;
    string line, name;
    while ( getline( cin, line ) ) {
        stringstream strin( line );
        strin >> age;
        getline( strin, name );
        }
    return 0;
}
原文地址:https://www.cnblogs.com/o-v-o/p/10154832.html