mardi 25 avril 2017

Vector gives 0 as output when out of bounds? [duplicate]

This question already has an answer here:

I was playing around with nested vectors and printing random elements to see the output and I noticed that when I go out of bounds I don't get an error. I simply get 0 returned as the value.

Why is this?

Is the vector being resized and initialized to zero whenever I attempt to call an out-of-bounds element by index?

Here's the code I was running:

#include <iostream>
#include <vector>


int main(){
  std::vector<std::vector<int>> matrix;
  std::vector<int> temp;
  temp.push_back(1);
  temp.push_back(2);
  matrix.push_back(temp);
  std::cout << matrix[0][45] << std::endl;

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire