-3

My c++ programs are not working when using vectors. The code of my program is given below, I am able to compile the file but when I try to run it just throws an error.

#include <iostream>
#include <vector>

int main(){
    std::vector<int> a;
    a[0]= 10;

    std::cout << a[0] << std::endl;

    a[0]= a[0]*10+5;
    std::cout << a[0] << std::endl;
}

I have tried commenting the lines with vector and writing another program in the same file but as soon as I uncomment the vector lines it throws an error on running.

I using VScode and MINgw.

  • 5
    Because you access vector element out of bounds. Default `std::vector` constructor creates an empty vector. – pptaszni Aug 22 '23 at 12:54
  • 1
    You have an empty vector, you cannot access any elements in it because there are none. – Yksisarvinen Aug 22 '23 at 12:54
  • Similar: https://stackoverflow.com/questions/47491286/how-reserve-in-stdvector-works-accessing-vector-with – pptaszni Aug 22 '23 at 12:55
  • Or initialize your vector first... e.g. `std::vector values{10,20,30};` after that it is safe to access elements [0], [1] and [2]. Lookup vector's constructor documentation here : https://en.cppreference.com/w/cpp/container/vector/vector – Pepijn Kramer Aug 22 '23 at 12:57
  • 2
    `std::vector a` creates `a` with zero elements. Accessing `a[0]` therefore gives undefined behaviour. You need to resize `a` to be (at least) `1` before using `a[0]` (e.g. `a.resize(1)` before the first usage of `a[0]`. More generally, rather than resorting to guesswork (and guessing wrong about how things work, as you have) try reading documentation for `std::vector` or a decent tutorial – Peter Aug 22 '23 at 12:58
  • BTW: you can change the vector to `std::map` and it will work since map contract items when first accessed. – SHR Aug 22 '23 at 12:58
  • @SHR which is imo one of the few design flaws in the standard library ;) And it distracts from what OP is asking about. – Pepijn Kramer Aug 22 '23 at 13:00
  • 1
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242) – Evg Aug 22 '23 at 13:01
  • @OP `a[0]= 10;` -- Change that to `a.at(0) = 10;`. What error do you get now? [Do you understand what that error is telling you?](https://godbolt.org/z/1b8zj41G4) – PaulMcKenzie Aug 22 '23 at 13:04
  • Read this: https://en.cppreference.com/w/cpp/language/ub - including all the very nice linked articles at the end. – Jesper Juhl Aug 22 '23 at 15:33

1 Answers1

2

The line:

std::vector<int> a;

creates an empty vector without any element.

According to cppreference: https://en.cppreference.com/w/cpp/container/vector/operator_at

a[0]= 10;

returns a reference to the element at a specified location - 0. No bounds checking is performed.

It does not mean: "Create element at position 0 and assign 10" - it assumes you have already created an element and saved new value to it"

What you want to do is:

std::vector<int> a; // create empty vector
a.push_back(10);    // add new value to vector

or alternatively

std::vector<int> a (1); //Constructs the container with count default-inserted instances of T (0 in our case)

std::cout << a[0] << std::endl; // It should return 0

a[0] = 10; 

std::cout << a[0] << std::endl;// It should return 10 now
UnclePooh
  • 64
  • 7