Re: Read data into c++
=D0=BF=D1=8F=D1=82=D0=BD=D0=B8=D1=86=D0=B0, 2 =D0=B0=D0=B2=D0=B3=D1=83=D1=
=81=D1=82=D0=B0 2013 =D0=B3., 4:17:44 UTC+4 =D0=BF=D0=BE=D0=BB=D1=8C=
=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8C axc...@gmail.com =D0=BD=D0=
=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB:
Hi all,
I have a data that looks like this:
1,2,3
1,3,4
2,4
1
2,4,5,6
I have these data in an excel file but can also copy into a txt file. I h=
ave declared a vector in c++ and want to read these data into my c++ code. =
For small problems, I typed it manually in my c++ code as:
int init1[] = {1,2,3};
std::vector<int> row1( init1, init1+sizeof(init1)/sizeof(init1[0]));
P.push_back(row1);
int init2[] = {1, 3, 4};
std::vector<int> row2( init2, init2+sizeof(init2)/sizeof(init2[0]));
P.push_back(row2);
and so on.
How can I import these data from txt or excel file?
Thanks
Here is a code snip that demonstrates how the task can be performed
[code]
std::string buffer = "1,2,3\n1,3,4\n2,4\n1\n2,4,5,6";
std::istringstream simulate_file( buffer );
std::string line;
std::vector<std::vector<int>> v;
while ( std::getline( simulate_file, line ) )
{
std::istringstream is( line );
std::string s;
std::vector<int> v1;
while ( std::getline( is, s, ',' ) ) v1.push_back( std::stoi( s ) );
v.push_back( v1 );
}
for ( const std::vector<int> &v1 : v )
{
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
}
[/code]