I came across a piece of C++ code in one of my projects that initializes a vector with two inputs.
One of the inputs is an existing array, and the other is the same array plus the array length.
I found a similar piece of code on another site:
// Create an array of string objects
std::string arr[] = {"first", "sec", "third", "fourth"};
// Initialize vector with a string array
std::vector<std::string> vecOfStr(arr, arr + sizeof(arr)/sizeof(std::string));
for(std::string str : vecOfStr)
std::cout << str << std::endl;
Can someone explain what arr + sizeof(arr)/sizeof(std::string)
is?
The website that this code was referenced in said that this was the corresponding constructor used:
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());