Noobgineer
Noobgineer

Reputation: 768

Pushing vector elements to a queue

I have a function which returns a std::vector. I want to push each element of the vector into a std::queue. Is the following expression correct:

myQueue.push(myVector);

If it is legal, will it add each element of the vector individually to the queue? Or will it add the entire vector to the queue and then i can use myQueue.front() to return the vector and access the elements within the vector?

OR

Do i have to iterate over the vector and push each element to the queue, i.e.

for(int i=0, i<myVector.size(); i++)
{
   myQueue.push(myVector[i]);
}

Thanks,

Upvotes: 0

Views: 1806

Answers (2)

Ren
Ren

Reputation: 2946

You have to iterate over the vector and push each element to the queue.

Upvotes: 1

Rob L
Rob L

Reputation: 2430

You do need to loop over the elements. (There other ways with inserters, but they all have to loop because these two containers are different types.) You have it fine, but I'd use new C++ syntax.

for (auto &entry: myVector)
    myQueue.push(entry);

Upvotes: 2

Related Questions