0

I'm coming from Java, and I'm now trying to initialize a vector in C++. I found a good way from this guy's answer. However, I don't know why it works.

I looked up the documentation for the constructor summary of vector and found this: screenshot

The last constructor is the one used in the the thread, and is shown here in my code:

#include "iostream"
#include "vector"

using namespace std;

int main()
{
    static const int arr[] = {1, 2, 3};
    vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));

    return 0;
}

How can it be that the new vector vec is initialized by copying the elements from "begin" to "end" if begin is just the c-array, and end is essentially the number of elements of the array, plus the memory allocated to arr. Maybe this documentation is too ambiguous, and this is really simple. Can someone at least point me to better documentation? Thanks.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • It handles pointers and treats them like the beginning and end of an array. All it's doing to whatever it gets is `++foo` (or `foo++` I suppose) and `*foo`. This works with pointers and iterators both. As for the documentation, I've never heard of it, but I adore http://en.cppreference.com/w/cpp. It also [links](http://en.cppreference.com/w/cpp/concept/InputIterator) to the requirements for the type that `begin` and `end` are. – chris Jan 16 '14 at 02:04
  • `begin`/`end` need to be iterator, and pointers are iterator – Bryan Chen Jan 16 '14 at 02:13
  • @BryanChen, They qualify as random-access iterators at least. – chris Jan 16 '14 at 02:20
  • Your image has gone. – Ant Aug 27 '20 at 10:08

1 Answers1

0

arr[] is an array and arr is a pointer on the data contained in this array. Containers in the standard library use iterators to access data and these have been modeled after pointers so that's why the vector's constructor can work with pointers as well.

Adding value to a pointer is called pointer arithmetic. Compiler knows the size of the object contained in the array so it automatically adds the right object size to pointer when incrementing. By default arr points to the first element of the array, arr + 1, to the second one, etc.

sizeof(arr[0]) is basically the size of the elements within the array. sizeof(arr) is the entire size in bytes of the array so in order to use pointer arithmetic, you need to know the number of element in the array so the iteration stops at the end. Since the array size is not defined as a constant in the code arr+sizeof(arr)/sizeof(arr[0]) is a way to calculate it.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
  • That took me a long time to understand, and I can say that your link is one of the most informative I've ever been forwarded. I think I get it, so I'll try to summarize, and hopefully you'll have the time to check me: `vector(begin, end)` works with `vector vec(arr, arr + sizeof(arr) / sizeof(arr[0]))` because it declares a vector of type int, then finds the address from the point at "begin" which is `arr` because arr is a pointer (which is an address or can be used like an address?), so then it just does this `vec[i] = arr + i * sizeof(T)` until it reaches the "end" address. – michaelsnowden Jan 16 '14 at 07:16
  • Nevermind on the parenthetical part about pointer/address confusion; i found [this](http://stackoverflow.com/questions/15151377/what-exactly-is-a-c-pointer-if-not-a-memory-address) – michaelsnowden Jan 16 '14 at 07:18