繁体   English   中英

从cin读取getline到stringstream(C ++)

[英]Reading getline from cin into a stringstream (C++)

所以我试图从标准输入(使用cin )读取这样的输入:

亚当英语85
查理数学76
埃里卡历史82
理查德科学90

我的目标是最终将每个数据块存储在我自己创建的数据结构中,因此基本上我想解析输入,因此每个数据都是单独的。 由于每行输入由用户一次输入一次,因此每次我得到需要解析的整行输入。 目前我正在尝试这样的事情:

stringstream ss;
getline(cin, ss);

string name;
string course;
string grade;
ss >> name >> course >> grade;

我遇到的错误是XCode告诉我没有匹配函数调用getline这让我感到困惑。 我已经包括了string库,所以我猜的错误与使用做getline读取从cinstringstream 任何帮助在这里将不胜感激。

你几乎就在那里,错误最有可能是1因为你试图用第二个参数stringstream调用getline ,只需稍作修改并先将数据存储在stringstd::cin中然后用它来初始化一个stringstream ,您可以从中提取输入:

// read input
string input;
getline(cin, input);

// initialize string stream
stringstream ss(input);

// extract input
string name;
string course;
string grade;

ss >> name >> course >> grade;

1.假设你已经包括:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

你不能std::getline()一个std::stringstream ; 只有一个std::string 作为字符串读取,然后使用字符串流来解析它。

struct Student
{
  string   name;
  string   course;
  unsigned grade;
};

vector <Student> students;
string s;
while (getline( cin, s ))
{
  istringstream ss(s);
  Student student;
  if (ss >> student.name >> student.course >> student.grade)
    students.emplace_back( student );
}

希望这可以帮助。

你可以使用cin >> name >> course >> grade; 因为>>无论如何都会读到空格。

您在代码中没有using namespace std ,或者您没有完全限定使用std::前缀在std命名空间中对API进行的调用,例如std::getline() 下面的解决方案解析CSV而不是标记化其中包含空格的值。 标量提取,解析CSV以及将等级从字符串转换为int的逻辑都是分开的。 regex_token_iterator用法可能是最复杂的部分,但它在大多数情况下使用非常简单的正则表达式。

// foo.txt:

// Adam,English,85
// Charlie,Math,76
// Erica,History,82
// Richard,Science,90
// John,Foo Science,89

// after compiling to a.exe, run with:
// $ ./a.exe < foo.txt 

// output
// name: Adam, course: English, grade: 85
// name: Charlie, course: Math, grade: 76
// name: Erica, course: History, grade: 82
// name: Richard, course: Science, grade: 90
// name: John, course: Foo Science, grade: 89

#include <iostream>
#include <sstream>
#include <regex>
#include <vector>

using namespace std;

typedef unsigned int uint;

uint stoui(const string &v) {
   uint i;
   stringstream ss;
   ss << v;
   ss >> i;
   return i;
}

string strip(const string &s) {
   regex strip_pat("^\\s*(.*?)\\s*$");
   return regex_replace(s, strip_pat, "$1");
}

vector<string> parse_csv(string &line) {
   vector<string> values;
   regex csv_pat(",");
   regex_token_iterator<string::iterator> end;
   regex_token_iterator<string::iterator> itr(
      line.begin(), line.end(), csv_pat, -1);
   while (itr != end)
      values.push_back(strip(*itr++));
   return values;
}

struct Student {
   string name;
   string course;
   uint grade;
   Student(vector<string> &data) : 
      name(data[0]), course(data[1]), grade(stoui(data[2])) {}
   void dump_info() {
      cout << "name: " << name << 
      ", course: " << course << 
      ", grade: " << grade << endl;
   }
};

int main() {
   string line;
   while (getline(cin, line)) {
      if (!line.empty()) {
         auto csv = parse_csv(line);
         Student s(csv);
         s.dump_info();
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM