2016-06-28 64 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,但是讓它們保持在你的腦海中。一旦你知道你對迭代器回到他們感到滿意。你會發現他們不可能沒有生活。 –