vector with 3 column matrix

Hi guys,

I want to load the information from a file into a vector of a class type Voter.
The file is basically a matrix though (ie. 3 columns) so not sure how to go about this?
Any help would be much appreciated!
So far I have the following, but this only pulls in the first column

thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    string RollFileName = "VotersRoll.dat";
    ifstream inFile_VotersRoll;
    inFile_VotersRoll.open(RollFileName);
    
    if(inFile_VotersRoll.fail())
    {
        printf("The VotersRoll.dat file failed to open.\n");
        return EXIT_FAILURE;
        
    }
    //connect to in file, VotersROLL file
    
    
    
    vector<Voter> voters;
    
    Voter input;
    
    while (inFile_VotersRoll>>input)
    {
        voters.push_back(input);
    }
    
    for (auto const &voter:voters)
    {
        cout << voter << endl;
    }
How is the Voter class defined? How is the Voter::operator>>( ... ) defined? That would determine how your code would work, you could use an overload like such

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Voter
{
    Voter( int, int, int ); //define this
    Voter(): Voter( 0, 0, 0 ) {}
    friend std::istream& operator>>( std::istream & is, Voter & voter ){ return is >> voter.x >> voter.y >> voter.z; }
    friend std::ostream& operator<<( std::ostream & os, Voter const &voter ){ return os << voter.x << voter.y << voter.z; }

private:
    int x, y, z;
};

int main()
{
    std::vector<Voter> voters;
    Voter input;

    while( file >> input ){ // I assume file is the std::ifstream object.
        voters.push_back( input );
    }
    for( Voter const & v: voters ) std::cout << v;
    return 0;
}
Topic archived. No new replies allowed.