解析输入c++的行

parsing line of input c++

本文关键字:的行 c++ 输入      更新时间:2023-10-16

我正在编写一个程序来进行深度优先搜索。是的,这是家庭作业,但我有问题的部分不是作业的学习点。我收到了格式不正确的输入,并且在将输入分离为有效变量并存储它们时遇到了问题。当我说格式错误的数据时,我的意思是有效数据之间没有特定数量的空格,并且每行的变量数不一致。。。试图找到一种更优雅的方式来获得我需要的数据,而不是3个循环。非常感谢您的帮助。我当前的代码:

int main()
{
    //read in first line (# of lines WITH data)
    cout << "enter the number of linesn";
    cin >> lines;
    //if non integer entered it won't error now
    while(!cin)
    {
        cin.clear(); // clears the error flags
        cin.ignore(20, 'n'); //flush the buffer
        cout <<"ndid not enter a valid integer, please try againnn";
        //reprompt for VALID input
        cout << "enter the number of linesn";
        cin >> lines;
    }
    //lines should now have a valid int input value
    //eat blank line
    getline(cin,str);

    //read in (# of lines WITH data retrieved from first input)
    for(int i = 0; i < lines; i++)
    {
        getline(cin, str);// read first string
        //process str
        //while not end of line
        //{
    
        //breakup line into individual variables
        std::string delim = " "; //set space as a delimiter
        size_t pos = 0;
        string token;
        while ((pos = str.find(delim)) != std::string::npos)
        {
            token = str.substr(0,pos);
            //set token to variable that increases (array location?)
            //get char, loop to check if chare = " ", eat it if it is until its not
        }
        //}
        //place variables in array location
        //...
    }
    //eat blank line at end of data set
    getline(cin,str);
    //alphabetize array
    // ...
    return 0;
}

样本输入:

11
Harry       Kate(18)       Fred(5)        Carol(6)
Alice      James(25)       Daisy(21)      Kate(10)
Carol     Fred(2)         Harry(6)       Daisy(12)
Ivy        James(16)       Bob(24)
Daisy       Carol(12)     Alice(21)     Elvis(28)
Elvis        James(18)       Daisy(28)      Fred(29)
Kate       Alice(10)      Fred(14)       Harry(18)    Gerald(20)
Fred        Kate(14)       Carol(2)     Harry(5)     Elvis(29)
Gerald      Kate(20)       Bob(17)    James(10)
James       Gerald(10)      Elvis(18)       Alice(25)   Ivy(16)
Bob     Ivy(24)        Gerald(17)

您可以用std::istringstream和std::distream_iterator标记每一行。下面给出了一个示例代码。

#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <sstream>
template <typename T>
std::vector<T> parse_line(const std::string& s) {
  std::vector<T> result;
  std::istringstream iss(s);
  std::copy(std::istream_iterator<T>(iss), std::istream_iterator<T>(),
            std::back_inserter(result));
  return result;
}
int main() {
  std::string s{"Harry       Kate(18)       Fred(5)        Carol(6)"};
  auto r = parse_line<std::string>(s);
  for (auto const& e : r) std::cout << e << std::endl;
  return 0;
}

也许你应该试试Boost,对于每一行:

string myline = "Harry       Kate(18)       Fred(5)";
vector<string> result;
boost::split(result, myline, boost::is_any_of(" "));

然后结果将包含例如:

 result[0] = Harry
 result[1] = Kate(18)
 result[2] = Fred(5)
 etc...

然后您需要遍历这个向量,我建议您使用regex来查看每个字符串是否包含括号。然后你可以得到名字和号码。