I read that std::vector should be contiguous. My understanding is, that its elements should be stored together, not spread out across the memory. I have simply acce
If you try coding it this way, you will see that the values remain the same and address for each value in the vector has a difference of 4 with its adjacent element (interesting).
std::vector numbers;
std::vector ptr_numbers;
// adding values 0 up to 8 in the vector called numbers
for (int i = 0; i < 8; i++) {
numbers.push_back(i);
}
// printing out the values inside vector numbers
//and storing the address of each element of vector called numbers inside the ptr_numbers.
for (int i = 0; i != numbers.size(); i++) {
cout << numbers[i] << endl;
ptr_numbers.push_back(&numbers[i]);
}
cout << "" << endl;
// printing out the values of each element of vector ptr_numbers
for (int y = 0; y != ptr_numbers.size(); y++) {
cout << *ptr_numbers[y] << endl;
}
// printing out the address of each element of vector ptr_numbers
for (int y = 0; y != ptr_numbers.size(); y++) {
cout << &ptr_numbers[y] << endl;
}
When you loop through both vectors. They will output the same values.