I'm learning how to use vectors in C++ using this post and they gave the example below on populating a vector using an array. The syntax is a bit confusing though.
#include <iostream>
#include <string>
#include <vector>
int main() {
// Array of string objects
std::string arr[] = {
"first",
"sec",
"third",
"fourth"
};
// Vector with a string array
std::vector < std::string > vecOfStr(arr,arr +sizeof(arr) / sizeof(std::string)); //This line is confusing
for (std::string str: vecOfStr)
std::cout << str << std::endl;
}
As far as my understanding goes, right after the variable name for the vector there comes the size of the vector. I would imagine that would have simply been sizeof(arr) / sizeof(std::string) but instead it's arr,arr +sizeof(arr) / sizeof(std::string). Can anyone tell me why?
std::vector < std::string > vecOfStr(std::begin(arr), std::end(arr));std::vector's constructor is overloaded, which means that, depending on the number and types of arguments it received, the behaviour is different.