0

I am new to using vectors in C++, my goal is to read a matrix from the text file and store them into a 2D vector, My code is as follows :

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
    std::ifstream in("input.txt");
    std::vector<std::vector<int> > v;

    if (in) {
        std::string line;

        while (std::getline(in, line)) {
            v.push_back(std::vector<int>());

            // Break down the row into column values
            std::stringstream split(line);
            int value;

            while (split >> value)
                v.back().push_back(value);
        }
    }

    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[i].size(); j++)
            std::cout << v[i][j] << ' ';

        std::cout << '\n';
    }
}

now for an input of say

10101010
01010101
10101011
01011010

I get an output of

10101010
1010101
10101011
1011010

i.e everytime a 0 is encountered in the beginning of a line it is omitted. I believe the problem is in the statement while(split>>value), but I dont know how to code it in a better way.

  • 1
    Leading 0's mean nothing to an integer so they are discarded. Why not just store the strings? – NathanOliver Oct 16 '17 at 20:14
  • I believe that is the problem because 0 is a logical false it is ommited, so how can I fix the code to include it ? – Ashwin Ramesh Oct 16 '17 at 20:15
  • I took the input file string by string because it is faster than reading it character by character, but I want to apply matrix operations later on so I want them to be stored as integers. – Ashwin Ramesh Oct 16 '17 at 20:17
  • Do you mean boolean instead of int? `10101010`, when read into an int, is a single integral value and not a series of integral values. – Stephan Lechner Oct 16 '17 at 20:18
  • I tried using boolean as my vector type, but because I am new to using vectors I could not split the string or traverse the vector because I believe boolean works bit by bit instead of int which works in bytes. – Ashwin Ramesh Oct 16 '17 at 20:20

2 Answers2

1

Replace

while (split >> value)
    v.back().push_back(value);

with

for(int x=0; x<line.size(); x++){
    v.back().push_back((int)line[x] - (int)'0'));
}

and remove your string stream completely.

pointerless
  • 753
  • 9
  • 20
  • can you explain the ' - (int)'0' ' part of the code please and thanks it worked – Ashwin Ramesh Oct 16 '17 at 20:27
  • so the ASCII standard defines the numbers as character in order, 0 to 9 so casting the character to an int then subtracting the value for 0 gives you the value of the character. – pointerless Oct 16 '17 at 20:29
  • Not a great explanation but look [here](https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c) – pointerless Oct 16 '17 at 20:29
1

It seems as if you actually want to store bits, but somehow struggled with parsing a line containing, for example, 10101010 into a series of bits. If you know the maximum number of bits per line, you could use bitset<N>, which provide an easy to use overload for operator >> that can directly read in something like 10101010. Hope it helps.

int main()
{
    std::ifstream in("input.txt");
    std::vector<std::bitset<8> >v;

    if (in) {
        std::bitset<8> bits;
        while (in >> bits) {
            v.push_back(bits);
        }
    }

    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[i].size(); j++)
            std::cout << v[i][j] << ' ';

        std::cout << '\n';
    }
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58