How i can move element of dynamic vector in argument of function push_back for dynamic vector

28 views Asked by At

how i can move element of dynamic vector in argument of function push_back for dynamic vector. Probably, i’ve said something and it isn’t right. Sorry, but i don't know English so good…

vector<int> *ans = new vector<int>();
vector<int> *x = new vector<int>();
ans->push_back(x[i]);
2

There are 2 answers

2
273K On

The simplest

ans->push_back(x->at(i));

But I don't understand why you call that "move element."

BTW vector<int>* is nonsense. x manages a dynamically allocated memory and vector<int>* never looks good.

0
Remy Lebeau On

x is a pointer to a single vector. You need to deference the x pointer before you can apply operator[] to that vector, eg:

vector<int> *ans = new vector<int>();
vector<int> *x = new vector<int>();
// put some elements in *x, then...
ans->push_back((*x)[i]);