c++ - 用空白填充二维数组(从文件输入)

标签 c++ arrays loops for-loop indexing

我在向二维数组“项目”添加空格时遇到问题。最后,我基本上希望文件 (quote.txt) 中的数据能够使用其行号正确索引。我已经将数组 9(row) 乘以 20(col),这是我文件中最大的句子,如果没有 20 列数据单元,我想用空格填充它,这样我就可以相应地索引我的数组。

我试过使用 vector 的 vector ,但它变得非常困惑。

  #include <iostream>
  #include <sstream> 
  #include <fstream>
  #include <vector>
  #include <array>
  #include <string>

  using namespace std;


  int main() 
  {
  string file_name;
  ifstream fin("quote.txt");
  while(!fin)
  {
      cout << "Error Opening File! Try again!" << endl;
      cout << "Enter file name: ";
      cin >> file_name;
  }
  string item[9][20];

  for (int row = 0; row < 9; row++) 
  {
      for (int col = 0; col < 20; col++)
      {
          fin >> item[row][col]; 
        //cout << item[row][col] << endl;

      }
  }
  for (int k = 0; k < 20; k++)
  {
    cout << item[0][k] << endl;
  }

  }

说明:我试图用 quote.txt 中的内容填充我的项目 2d 数组,但由于句子长度不同,我不能使用 for 循环并说列是 20 个单位,因为它会渗入下一行并搞砸了索引。我的解决方案是我想添加一个空格(填充符),以便我可以使用我的 for 循环进行迭代,并且每行中的每个内容都有 20 列。这样我就可以使用行索引来查看文本文件中的每一行。基本上,我希望文本文件是一个二维数组,我可以通过使用 [row][col] 索引在其中找到每个元素(单词)。

文本文件:“quote.txt”

    People often say that motivation doesn  t last   Well   neither does bathing that s why we recommend it daily   Ziglar
    Someday is not a day of the week      Denise Brennan  Nelson
    Hire character   Train skill      Peter Schutz
    Your time is limited   so don t waste it living someone else s life      Steve Jobs
    Sales are contingent upon the attitude of the salesman      not the attitude of the prospect      W   Clement Stone
    Everyone lives by selling something      Robert Louis Stevenson
    If you are not taking care of your customer   your competitor will      Bob Hooey
    The golden rule for every businessman is this: Put yourself in your customer s place      Orison Swett Marden
    If you cannot do great things do small things in a great way    Napoleon Hill

程序应该做什么?

该程序假设允许我通过用户输入查找单词。说这个词是“of”,它应该输出它所在的行号。同样,如果我输入“of People”,它会输出行号

最佳答案

我可能会这样做:

// The dynamic vector of strings from the file
std::vector<std::vector<std::string>> items;

std::string line;

// Loop to read line by line
while (std::getline(fin, line))
{
    // Put the line into an input string stream to extract the "words" from it
    std::istringstream line_stream(line);

    // Add the current line to the items vector
    items.emplace_back(std::istream_iterator<std::string>(line_stream),
                       std::istream_iterator<std::string>());
}

在此之后,items vector 将包含所有行上的所有单词。例如 items[1] 将是第二行,items[1][2] 将是第二行的第三个词(“not” 与您显示的文件内容)。


按照程序的既定目的(在文件中查找单词或短语,并报告找到它们的行号),您根本不需要存储这些行。

您需要做的就是将每一行读入一个字符串,将所有制表符和多个空格替换为一个空格,然后查看是否在该行中找到了单词(或短语)。如果找到,则将行号存储在 vector 中。然后在阅读下一行时丢弃当前行。

处理完所有文件后,只需报告 vector 中的行号即可。

关于c++ - 用空白填充二维数组(从文件输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065786/

相关文章:

c++ - shellcode 在单独运行时调用不同的系统调用

c++ - 你有吗?你如何进行 C++ 自动测试?

php变量导致while循环无限循环显示数据

javascript - 检查 Ember.js 中每个循环中的重复项

c++ - 如何在 C++ 代码/项目中查找内存泄漏?

c++ - PyString_FromStringAndSize 导致段错误

php - 我可以在 url 中将 $_get 值作为数组传递吗?

javascript - Ng-repeat 不更新数组更改

javascript - 如何像彩票一样抽出总是不同的数字?

javascript - 运行一个循环,然后重置并再次运行