duck
duck

Reputation: 1904

printing a 2d vector pointer

I created a 2d Vector like so :

int main(){
   std::vector<std::vector<int> > my_2d_Vector;

   for(int i = 0; i < 10; ++i){
      std::vector<int> internal_vector;
      for(int j; j < 10; ++j){
         internal_vector.push_back(j);
      }
      my_2d_vector.push_back(internal_vector);
   }
 print_2d_vector(&my_2d_vector);
}

And i am trying to print it to the screen like so :

void print_2d_vector(std::vector<std::vector<int> > *my_vector){
   for(int i = 0; i < my_vector->size(); ++i){
      for(int j = 0; j < my_vector[i].size(); ++j){
         std::cout << my_vector[i][j];
      }
    std::cout << "/n";
   }
}

This looks fine too me. But for some reason i dont understand, it tells me this when i try to compile it : error : no match for 'operator<<' (operand types are std::ostream {aka std::basic_ostream<char>} and 'std::vector<int>')

I am confused as i am passing it an int from a vector and not the vector itself ?

Upvotes: 1

Views: 3824

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

If you have a pointer to a 2D vector like this

std::vector<std::vector<int> > *my_vector;

then dereferencing the pointer *my_vector you will get the original vector itself.

Thus expression ( *my_vector )[i] gives the i-th element of the outer vector and expression ( *my_vector )[i][j] gives the jth element of the ith inner vector.

Thus the loops can look like

void print_2d_vector(std::vector<std::vector<int> > *my_vector){
   for(int i = 0; i < my_vector->size(); ++i){
      for(int j = 0; j < ( *my_vector )[i].size(); ++j){
         std::cout << ( *my_vector )[i][j];
      }
    std::cout << "/n";
   }
}

It is the same as

void print_2d_vector(std::vector<std::vector<int> > *my_vector){
   for(int i = 0; i < my_vector->size(); ++i){
      for(int j = 0; j < my_vector->operator []( i ).size(); ++j){
         std::cout << my_vector->operator []( i )[j];
      }
    std::cout << "/n";
   }
}

Take into account that you could declare the function as having the parameter of the type std::vector<std::vector<int> > &. In this case the function will look simpler. For example

std::ostream & print_2d_vector( const std::vector<std::vector<int> > &my_vector,
                                std::ostream &os = std::cout )
{
    for ( size_t i = 0; i < my_vector.size(); ++i )
    {
        for ( size_t j = 0; j < my_vector[i].size(); ++j )
        {
            os << my_vector[i][j] << ' ';
        }
        os << "/n";
    }

    return os;
}

Using this function you could for example to write the vector in a file.

Upvotes: 0

Rakete1111
Rakete1111

Reputation: 48998

I am confused as I am passing it an int from a vector and not the vector itself?

No, you are not. You would actually have undefined behavior if your program compiled.

Because my_vector is a pointer to a std::vector<std::vector<int>>, this my_vector[i][j] evaluates to the ith 2d vector in the array my_vector, returning the jth vector in the ith 2d vector.

You'll need to dereference my_vector first:

std::cout << (*my_vector)[i][j];

Upvotes: 2

Hatted Rooster
Hatted Rooster

Reputation: 36503

It's because you pass a pointer to the function:

my_vector[i]

That doesn't actually access the vector, but rather the pointer. Then the second one, [j] accesses the actual vector. You need to dereference the pointer first to access the vector.

Why do you even require a pointer anyway? Just pass by value or use a reference if you need to have the function changing the vector:

void print_2d_vector(std::vector<std::vector<int> >& my_vector){
   for(int i = 0; i < my_vector->size(); ++i){
      for(int j = 0; j < my_vector[i].size(); ++j){
         std::cout << my_vector[i][j];
      }
    std::cout << "/n";
   }
}

And:

print_2d_vector(my_2d_vector);

Upvotes: 4

Related Questions