简体   繁体   中英

How to access value inside vector of vectors of type class

I'm in an introductory C++ class and we've been tasked to write a program that reads a .ppm picture file, copies it, then writes it to an output file.

To do this, my PpmPicture class has a vector of vectors, where each index contains a Pixel with red, green, and blue ints.

My problem is with my output function, where I'm trying to output those ints to a file. How do I go about accessing them individually? Here is my code, you can see where I'm trying to output those values at the bottom in my writePpmFile function. I know the picture.red[0][0] doesn't make any sense, I was just trying to brute force a solution and that happened to be the last thing I tried.

#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;
}

I should add, like I said this is an introductory class so we haven't gone over a lot of shortcuts/different functions. So if possible keeping it somewhat simple (even if it isn't that efficient) would be preferred :)

This is mostly a question of applying what you already know, but it seems like you're doing it in a sightly odd order.

Since picture is a vector<vector<Pixel>> , picture[i] is a vector<Pixel> and picture[i][j] is a Pixel .

The components of a Pixel are always accessed the same way – pixelvalue.componentname – so the components of this Pixel are picture[i][j].red , picture[i][j].green , and picture[i][j].blue .

A better approach to this would be writing an insertion operator for Pixel for example:

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

Given this you can stream all of vector<Pixel> picture to ostream fout with only the command:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM