I am trying to understand the "contiguous" nature of vectors and where they assign their values in memory.
Can someone please explain why the 2nd cout yields a 1 instead of a 4?
std::vector<int> test = { 2,13,4 };
std::cout << &test[2] << " " << &test[1] << std::endl;
std::cout << &test[2] - &test[1] << std::endl;
An int on my machine is 4 bytes large.
So that means test[2] is 4 bytes away from test[1] in memory.
if I write:
std::cout << (int)&test[2] << " " << (int)&test[1] << std::endl;
std::cout << (int)&test[2] - (int)&test[1] << std::endl;
Then I get 4...
Can someone explain why the first example yields 1?
Thank you.