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.