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:
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.