解析输入c++的行
parsing line of input c++
我正在编写一个程序来进行深度优先搜索。是的,这是家庭作业,但我有问题的部分不是作业的学习点。我收到了格式不正确的输入,并且在将输入分离为有效变量并存储它们时遇到了问题。当我说格式错误的数据时,我的意思是有效数据之间没有特定数量的空格,并且每行的变量数不一致。。。试图找到一种更优雅的方式来获得我需要的数据,而不是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
来查看每个字符串是否包含括号。然后你可以得到名字和号码。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 对对应于矩阵的行和列的对向量进行排序
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 双击更改 mfc 中列表控件中的行的颜色
- 根据新的行分隔符从字符串中删除子字符串
- 绘制二维矩阵的行和列
- 将 MatrixXd 的行传递给要修改的函数,而不在 Eigen 中创建副本
- 如何在循环中打印两个相邻的行,同时在 c++ 中将它们都保留在一行中
- 如何在 DevC++ 上显示带有中文字母的行?
- 如何使用循环结束 c++ 上的行
- 使用 range-v3 读取包含逗号分隔数据的行
- 我如何根据 c++ 中的行合并两个 2D 数组
- 使用 C++将文本光标移动到屏幕中不可见的行
- 获取上次执行的返回语句的行
- 当在C++中找到所需的行时,如何从文件中获取接下来的 n 行
- 如何将矩阵的行随机复制到内存中的另一个矩阵的过程并行化?
- 如何删除 Eigen3 矩阵中某些不连续的行和列?
- C++:交换矩阵的行:使用单维数组
- 获取要从文本文件中读取的行