1

I am making some testing of vectors arrays and I don't know how to print it. Here is my code:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include "vector"
#include <windows.h>

using namespace std;

vector<vector<int>> generateArrays(){

vector<vector<int>> cisla;

for(unsigned int i=1; i < 11; i++){
    vector<int> pole;
    vector<int> pole2;
    for(unsigned int j=0; j < i*5; j++){
        int a = rand();
        pole.push_back(a);
        pole2.push_back(a);
    }
    cisla.push_back(pole);
    cisla.push_back(pole2);
}
return cisla;
}

vector<vector<int>> arrays = generateArrays();


void Print (const vector<int>& arrays){
  // function for prinitng arrays
}  


int main(){
    Print(arrays);
  system("pause");
}

What I need is some function to write down all numbers in vector arrays. I tried to Google it but none of the code work for me.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
user2466601
  • 207
  • 1
  • 7
  • 19

8 Answers8

2
// requires #include <algorithm> for std::copy
// requires #include <iterator> for std::ostream_iterator
void Print(const vector<vector<int>>& arrays, std::ostream& out = std::cout)
{
    for(const auto& array:arrays) {
       std::copy(array.begin(), array.end(), std::ostream_iterator<int>(out, " "));
       out << std::endl;
    }
} 
utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

You can use vector::iterator, for instance:

vector<vector<int>>::iterator i = arrays.begin();
vector<int>::iterator j = *i.begin();

for(;i != arrays.end(); ++i) {
  for(;j != *i.end(); ++j) {
     std::cout << *j << " ";
  }
  std::cout << "\n";
}  
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • 1
    I think the posted code will not work as expected because j is never reset to *i.begin() inside the outer for-loop. – duselbaer Jan 30 '14 at 12:03
  • It would be more idiomatic to initialise the iterators in the `for`-statement's initialiser clause. In any case, you'll need to reinitialise `j` for each iteration of the outer loop. Also, that should be `i->begin()` or `(*i).begin()`, not `*i.begin()`. – Mike Seymour Jan 30 '14 at 12:19
0

What about this?

Create a stream output operator for vector of T like this:

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const & array)
    bool seenFirst = false;
    os << "[ ";
    for (auto const & i : array)
    {
       if (!seenFirst)
       {
          seenFirst = true;
          os << i;
       }
       else
       {
          os << ", " << i;
       }
    }
    os << "]";
    return os;
}

At the end you might use it for std::vector<int> as well as for std::vector< std::vector <int> > like this:

std::cout << arrays;
duselbaer
  • 935
  • 2
  • 6
  • 10
0

If you have Print prototype as shown then,

void Print (const vector<int>& arrays){
  for(auto x:arrays)
   std::cout << x <<" ";
} 

int main(){

  for(const auto& array:arrays)
    Print(array);

  system("pause");
}

Otherwise you can combine both in one function like

void Print (const vector<vector<int>>& arrays){

  for(const auto& array:arrays)
   for(auto x:array)
    std::cout << x <<" ";
} 
P0W
  • 46,614
  • 9
  • 72
  • 119
0

As you have std::vector<std::vector<int>> then the function could look as

void Print( const std::vector<std::vector<int>> &arrays )
{
   for ( const std::vector<int> &v : arrays )
   {
      for ( int x : v ) std::cout << x << ' '; // you can include std::setw here
      std::cout << std::endl;
   }
} 

Or if you need to output only std::vector<int> then it will look as

void Print( const std::vector<int> &arrays )
{
   for ( int x : arrays ) std::cout << x << ' '; // you can include std::setw here
} 

If your compiler does not support the range based for statement then you can use for example standard algorithm std::copy.

void Print( const std::vector<int> &arrays )
{
   std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );
} 

Or you can use an ordinary loop

void Print( const std::vector<int> &arrays )
{
   for ( std::vector<int>::size_type i = 0; i < arrays.size(); i++ ) 
   {        
      std::cout << arrays[i] << ' '; // you can include std::setw here
   }
} 

Or with iterators

void Print( const std::vector<int> &arrays )
{
   for ( auto it = arrays.begin(); it != arrays.end(); ++it ) 
   {        
      std::cout << *it << ' '; // you can include std::setw here
   }
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
void Print (const vector<int>& arrays){
     for (std::vector<int>::iterator it = arrays.begin() ;
          it != arrays.end();
          ++it)
    std::cout << ' ' << *it;
}
beardedN5rd
  • 185
  • 6
0
#include <algorithm>

vector<vector<int>>::iterator it = arrays.begin();
while ( !(it == arrays.end()) {
    std::copy( (*it).begin(), (*it).end(), 
                                   std::ostream_iterator<int>( std::cout, ","));
    ++it;
} 
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

In C++11``

for (auto i = path.begin(); i != path.end(); ++i)
std::cout << *i << ' ';

for(int i=0; i<path.size(); ++i)
std::cout << path[i] << ' ';
Akash Kandpal
  • 3,126
  • 28
  • 25