簡體   English   中英

C ++將信息從文本文件加載到2D數組中

[英]C++ Loading information from text file into a 2D Array

我是一個非常新手的程序員,遇到了一些問題。 我需要使用存儲在文本文件中的數據加載2D數組。 文本文件的內容如下(兩個數字,然后是行的末尾。即1 1949,然后是下一行):


1949年1月1日

1972年2月2日

1983年3月3日

1959年4月4日

1987年5月5日

1991年6月6日

1995年7月

1991年8月8日

1957年9月9日

1980年10月10日

1995年11月11日

1995年12月12日


數組的格式應相同。 當然不是在這里尋找答案,而是朝着正確的方向前進。 我一直沒有用。 謝謝。

在這里,我使用pair<int, int>來存儲每一行​​; 如果您具有C ++ 11,則可以改用array<int, 2> 剩下的就是樣板,讀取和分割線。

#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
  assert(argc == 2);
  ifstream input(argv[1]);
  assert(input);

  vector<pair<int, int> > data;

  for (string line; getline(input, line); )
  {
    istringstream stream(line);
    data.resize(data.size() + 1);
    stream >> data.back().first >> data.back().second;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM