C++如何从文本文件读取列? 我有一个C++项目,需要从文本文件中找到最大的文件名。 我的文本文件示例是: foundedindex = instline.find(" "); inst_host = instline.substr(0, foundedindex); //cout << inst_host << " a" << endl; obj[count].sethost(inst_title);
所以,我想只读index.html,23,html,24.html等等。 当我在代码中把所有的行一行一行地分开时,排序花费了太多的时间。C++如何从文本文件读取列? 我有一个C++项目,需要从文本文件中找到最大的文件名。 我的文本文件示例是: foundedindex = instline.find(" "); inst_host = instline.substr(0, foundedindex); //cout << inst_host << " a" << endl; obj[count].sethost(inst_title);,c++,file,sorting,text,C++,File,Sorting,Text,所以,我想只读index.html,23,html,24.html等等。 当我在代码中把所有的行一行一行地分开时,排序花费了太多的时间。 请帮助我。这是一种获取每行列的方法 std::string temp; std::vector<vector<std::string>> data; while(std::getline(file, temp)) { std::vector<std::string> x; std::istringstream
请帮助我。这是一种获取每行列的方法
std::string temp;
std::vector<vector<std::string>> data;
while(std::getline(file, temp))
{
std::vector<std::string> x;
std::istringstream liney(temp);
while(std::getline(liney, temp, ' '))
{
x.push_back(temp);
}
data.push_back(x);
}
// Then using a column loop through a 2d array.
int row = 0;
for(int i = 0; i<data[row].size(); i++)
{
for(int s = 0; s<data.size(); s++)
{
data[s][i]; // Do something
}
row++;
}
希望这能有所帮助。请将代码、错误、样本数据或文本输出以纯文本形式发布在此处,而不是以图像形式发布,这些图像可能难以阅读,无法复制粘贴以帮助测试代码或用于答案,并且对使用屏幕阅读器的人怀有敌意。您可以编辑问题以在问题正文中添加代码。使用{}按钮格式化任何代码块,或使用四个空格缩进以获得相同效果。使用std::getline、std::istringstream和提取运算符>>仅获取输入行的一部分。您似乎将代码和数据混淆了。@user0042我编辑了我的文章以反映您的suggestion@user0042对不起,这应该是好的。@Alkyonemis如果这有效,请投票并接受。