从.txt文件中读取并将值输入到多维向量中

Reading from a .txt file and inputting values into multidimensional vector

本文关键字:输入 向量 txt 文件 读取      更新时间:2023-10-16

我是c++的新手,正在进行我的第一个项目,该项目涉及读取下面表格的.txt文件。我遇到的问题是将像素的值插入到2D动态表中,我稍后可以对其进行分析。我需要读取第一个像素的值,并将其放入表格的第一个元素中,将第二个像素放入表格的第二个元素中等等,直到我在一个高度为150、宽度为250的表格中拥有所有像素为止(注意,这只是一个示例,尺寸可能会根据.txt文件而变化)。

250 // width pixels
150 // height en pixels
2 // number of colours
205 // colour 0
35 // colour 1
0 // value of pixel 0, background colour (white)
0 // value of pixel 1, background colour (white)
…
205 // one pixel of colour 0 (red)
…
35 // one pixel of colour 1 (blue)
…
0 // value of last pixel, background colour

到目前为止,我的代码如下(编译):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main () {
    ifstream f_in;
    f_in.open("Pixmap.txt");
    int largeur;
    int hauteur;
    int nbre;
    if (f_in.is_open())
    {
        f_in >> largeur;
        f_in >> hauteur;
        f_in >> nbre;
    }

    else cerr << "Unable to open file";
    f_in.close();

    return 0;
}

任何帮助都将不胜感激。。。感谢

初始化向量;矢量点;现在创建循环

for (int i = 0; i < hauteur; i++) {
    for (j = 0; j < largeur; j++) {
        int tmp;
        f_in >>tmp;
        points.push_back(tmp);
    }
}

它必须在上运行

这是一个完整的解决方案。我添加了很多评论来帮助你理解正在发生的一切。我们假设文件格式正确(即你给它正确的格式)。

使用它,颜色被存储在颜色矢量中,像素数据被存储在数据矢量中。我们浏览文件1次,读取所有我们想要的值。

您可能需要查找一些用于更好地理解它们的函数。http://www.cplusplus.com/reference/string/string/getline/http://www.cplusplus.com/reference/vector/vector/http://mathbits.com/MathBits/CompSci/APstrings/APgetline.htm(这就是为什么我们必须使用伪getline)

我还在末尾添加了一些打印,以便使用迭代器打印出数据。如果有什么不清楚的地方,请告诉我。

测试数据:

3
2
2
205
35
1
1
1
2
2
2

这是程序:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  ifstream f_in;
  f_in.open("Pixmap.txt");
  //If its not open return with an error
  if (!f_in.is_open())
    cerr << "Error!";
  int width;
  int height;
  int numColours;
  //Read width and height and num of colours (first 3 lines from file)
  f_in >> width;
  f_in >> height;
  f_in >> numColours;
  vector<int> colours; //we will store the colours in here
  //Read in colours
  for (int c = 0; c < numColours; ++c) { //we have a loop that iterated numColours times
    int colour;
    f_in >> colour; //read in colour
    colours.push_back(colour); //push to the back of the vector so the read will be in order
  }
  //Make multidimentional vector
  //the data(height, vector<int>(width)) means we are initilizing the size of
  //of the inner vector. There are 'height' rows (ie height number of vector<int> and each
  //vector<int> is initilised to a vector<int>(width), ie vector<int> of size width
  //All the initial values will be 0
  vector<vector<int>> data(height, vector<int>(width));
  string input; //input string for reading lines
  int i = 0;
  int j = 0;
  //We need a dummy read so our stream points to the pixel data
  //We could do this in other ways but right now we will use getline to get a line but not save it
  //This is actually an issue with using >> operator before getline (see my link to read about it)
  getline(f_in, input);
  //We use getline in a loop like this. Get line will get 1 line from the file pointed to by the stream and store it
  //In the input string
  while (getline(f_in, input)) {
    data[i][j++] = stoi(input); //We store the data we need, stoi converts strings to integers
    //Once j has reached end of vector
    if (j == width) {
      ++i; //increase i (we are at a new row now)
      j = 0; //set j = 0, the width has been reached and we want to start at 0th spot on new line
    }
  }
  //Print out colours
  int colNum = 0;
  cout << "Num of Colours: " << numColours << endl;
  for (auto itc = colours.begin(); itc != colours.end(); ++itc) {
    cout << "Colour " << ++colNum << ": " << *itc << endl;
  }
  //Print out the vector
  for (auto it = data.begin(); it != data.end(); ++it) {
    for (auto it2 = it->begin(); it2 != it->end(); ++it2) {
      cout << *it2 << " ";
    }
    cout << endl;
  }
  //close stream
  f_in.close();
  return 0;
}