What is the simplest way to convert array to vector?
void test(vector<int> _array)
{
...
}
int x[3]={1, 2, 3};
test(x); // Syntax error.
I want to convert x from int array to vector in simplest way.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the
vectorconstructor that takes two iterators, note that pointers are valid iterators, and use the implicit conversion from arrays to pointers:or
where
sizeof x / sizeof x[0]is obviously3in this context; it’s the generic way of getting the number of elements in an array. Note thatx + sizeof x / sizeof x[0]points one element beyond the last element.