murthi murthi - 5 days ago 5
C++ Question

Converting an array of int, to a vector of int in c++

I have an array something like this

int A[] = {1, 2, 3, 4, 5, 6, 7}
vector<int> vec;
int index = 0;
for(int i = 0; i < A.size(); i++) {
if(some condition) {
index = i;
}
**vec(A + index, i);**
}


how to convert an array to vector starting from particular index, like above ?

P0W P0W
Answer

You can use the index as follows.

#include <iterator>

int A[] = {1, 2, 3, 4, 5, 6, 7};

int index = 0;
for(int i = 0; i < A.size(); i++) {
if( /* some condition */ ) {
    index = i;
    break;
}

}
std::vector<int> vec ( std::begin(A) + index, std::end(A) ) ;
Comments