How can I take an input from a 2D array into a vector

Hi,
I have an an array of integers lets say:

1 2 3
4 5 6

I want to take the input from this array into a vector of vectors say:
vector<vector<int> > v1;

How can I insert elements fro this array into my vector? Your help is appreciated.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;


using matrix = vector< vector<int> >;


//==========================================================


matrix readArray( string filename )
{
   matrix M;

   ifstream in( filename );
   string line;
   while ( getline( in, line ) )                   // read a whole line of the file
   {
      stringstream ss( line );                     // put it in a stringstream (internal stream)
      vector<int> row;
      int item;
      while ( ss >> item ) row.push_back( item );  // read all available items on this line
      if ( !row.empty() ) M.push_back( row );      // add non-empty rows to matrix
   }
   return M;
}


//==========================================================


void write( const matrix &M )
{
   const int w = 4;
   for ( auto row : M )
   {
      for ( auto e : row ) cout << setw( w ) << e << ' ';
      cout << '\n';
   }
}


//==========================================================


int main()
{
   matrix M = readArray( "data.txt" );
   write( M );
}


//========================================================== 


   1    2    3 
   4    5    6 





For the palooka-code version you could overload lots of << and >> operators:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iterator>
#include <vector>
using namespace std;


//==========================================================


template <typename T> istream &operator >> ( istream &strm, vector<T> &V )
{
   string line;
   if ( getline( strm, line ) )
   {
      stringstream ss( line );
      V = vector<T>( istream_iterator<T>{ss}, {} );
   }
   return strm;
}


//==========================================================


template <typename T> ostream &operator << ( ostream &strm, const vector<T> &V )
{
   const int w = 4;     // need something better than this
   for ( auto e : V ) strm << setw( w ) << e << ' ';
   return strm;
}


//==========================================================


template <typename T> istream &operator >> ( istream &strm, vector< vector<T> > &M )
{
   vector<T> row;
   while ( strm >> row ) if ( !row.empty() ) M.push_back( row );
   return strm;
}


//==========================================================


template <typename T> ostream &operator << ( ostream &strm, const vector< vector<T> > &M )
{
   for ( auto row : M ) strm << row << '\n';
   return strm;
}


//==========================================================


int main()
{
   vector< vector<int> > M;

   stringstream in( "1 2 3 \n 4 5 6 \n" );
   
   in >> M;
   cout << M;
}


Last edited on
Thanks so much.
Just a quick question

How can I have 1D column vector in C++? Like I want a vector as:
1
2
3
Exactly the same way you have the 1D row vector. Three numbers is three numbers.

How you use the vector makes the difference:
1
2
3
4
5
6
7
8
std::vector<int> V;

// print as column
for ( auto elem : V ) std::cout << elem << '\n';

// print as row
for ( auto elem : V ) std::cout << elem << ' ';
std::cout << '\n';
Last edited on
Topic archived. No new replies allowed.