Hi.
I'm trying to input 2-dimensional array data from command line (i.e. not file data).
My question is how to do that? For one dimension array, I can separate data with "space", but how should I do for 2-dimensional data?
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<std::vector<std::string> > k;
std::string line;
if(std::getline(std::cin, line))
{
if(! line.empty())
{
std::stringstream ss(line);
std::vector<std::string> v;
while(ss.good())
{
std::string s;
ss >> s;
v.push_back(s);
}
k.push_back(v); // place the 'push_back()' here if you want no empty lines otherwise outside this 'if' (move it under the following '}')
}
}
}
Then you can use k[i][j]
Sorry that I can't make it simplier. This might give you the idea: