2016-06-28 62 views
1

我在介绍性的C++类中,我们的任务是编写一个读取.ppm图片文件的程序,将其复制,然后将其写入输出文件。如何访问矢量类型的向量中的值

为此,我的PpmPicture类有一个向量向量,其中每个索引都包含一个带有红色,绿色和蓝色整数的像素。

我的问题是与我的输出功能,我试图将这些整数输出到文件。我如何去单独访问它们?这里是我的代码,你可以看到我在writePpmFile函数底部输出这些值的位置。我知道picture.red [0] [0]没有任何意义,我只是试图强制一个解决方案,而这恰好是我尝试的最后一件事。

#include <iostream> 
#include <vector> 
#include <fstream> 
#include <sstream> 
using namespace std; 

struct Pixel { 
    Pixel(); 
    Pixel(int redValue, int greenValue, int blueValue); 
    int red; 
    int green; 
    int blue; 
}; 

Pixel::Pixel() { 
    red = 0; 
    green = 0; 
    blue = 0; 
} 

Pixel::Pixel(int redValue, int greenValue, int blueValue) { 
    red = redValue; 
    green = greenValue; 
    blue = blueValue; 
} 

class PpmPicture { 
public: 
    PpmPicture(); 
    bool readPpmFile(string inputFile); 
    int writePpmFile(string outputFile); 
private: 
    vector<vector<Pixel> > picture; 
    int rows; 
    int columns; 
    int intensity; 
}; 

PpmPicture::PpmPicture() { 
    rows = 0; 
    columns = 0; 
} 

int main(int argc, char *argv[]) { 
    PpmPicture myPic; 

    if(argc != 3) { 
     cout << "usage: inputfile outputfile"; 
     return 0; 
    } 

    myPic.readPpmFile(argv[1]); 
    myPic.writePpmFile(argv[2]); 
} 

bool PpmPicture::readPpmFile(string inputFile) { 
    Pixel pix; 
    vector<Pixel> tempArray; 
    fstream fin; 
    string fileType; 
    int i, j; 

    fin.open(inputFile.c_str()); 
    //Check if file opened 
    if(fin.fail()) { 
     return false; 
    } 

    while(!fin.eof()) { 
     //Input first four values into appropriate variables 
     fin >> fileType >> columns >> rows >> intensity; 
     //Fill tempArray with pixel values 
     while(fin >> pix.red >> pix.green >> pix.blue) { 
      tempArray.push_back(pix); 
     } 
    } 

    //Read file until you reach the number of rows specified by the file 
    for(j = 0; j < rows; j++) { 
     //Input pixel values into each index in the row array 
     //Enter new row when one row's width is achieved 
     for(i = 0; i < columns; i++) { 
      picture[j].push_back(tempArray[i]); 
     } 
    } 

    return true; 
} 

int PpmPicture::writePpmFile(string outputFile) { 
    ofstream fout; 
    int i, j, temp; 

    fout.open(outputFile.c_str()); 
    if(fout.fail()) { 
     return -2; 
    } 

    if(columns < 0 || rows < 0) { 
     return -1; 
    } 

    fout << "P3" << endl; 
    fout << columns << rows << endl; 
    fout << intensity << endl; 
    fout << picture.red[0][0]; 
    fout << picture.green[0][0]; 
    fout << picture.blue[0][0]; 

    /*for(j = 0; j < rows; j++) { 
     for(i = 0; i < columns; i++) { 
      fout << picture[j][i] << " "; 
     } 
    }*/ 

    return 0; 
} 

我要补充,就像我说的,这是一个入门级,所以我们没有去了大量的快捷键/功能不同。因此,如果可能的话保持它有点简单(即使效率不高)将是首选:)

+0

想想研究所作为一个PpmPicture实例: inst.picture.at(i).at(k) –

+2

'picture [j] [i]'给你位置[i] [j]的'pixel'。你将如何获得常规像素实例的r,g,b值? – NathanOliver

+0

当然,它返回一个Pixel实例,也许你应该实现一个方法来检索RGB值为@NathanOliver建议 –

回答

0

这主要是一个应用你已知的问题,但它看起来像你在做一个很轻松的事情奇数次序。

由于picturevector<vector<Pixel>>picture[i]vector<Pixel>picture[i][j]Pixel

一个Pixel的组件总是访问同样的方式 - pixelvalue.componentname - 所以这Pixel的成分是picture[i][j].redpicture[i][j].greenpicture[i][j].blue

+0

你是对的,我肯定只是在试图弄清楚按照什么顺序进行操作。这个答案非常有意义,谢谢。 –

0

这更好的办法是为Pixel来编写insertion operator例如:

ostream& operator<<(ostream& lhs, const Pixel& rhs) { 
    lhs << rhs.red << ' ' << rhs.green << ' ' << rhs.blue; 
} 

考虑到这一点,你可以只命令传输所有的vector<Pixel> pictureostream fout

copy(cbegin(picture), cend(picture), ostream_iterator<Pixel>(fout, " ")) 
+0

不幸的是,我们还没有完成copy,cend或ostream_iterator。到达那里,但! –

+0

@Toy_Reid是的,我们从来没有把它放到我的C++类中的'ostream_iterator's,但是让它们保持在你的脑海中。一旦你知道你对迭代器回到他们感到满意。你会发现他们不可能没有生活。 –