简体   繁体   English

将向量元素推入队列

[英]Pushing vector elements to a queue

I have a function which returns a std::vector . 我有一个返回std::vector的函数。 I want to push each element of the vector into a std::queue . 我想将vector每个元素推入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? 还是将整个向量添加到队列中,然后我可以使用myQueue.front()返回向量并访问向量中的元素?

OR 要么

Do i have to iterate over the vector and push each element to the queue, ie 我是否必须遍历向量并将每个元素推入队列,即

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

Thanks, 谢谢,

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. (使用插入器还有其他方法,但是它们都必须循环,因为这两个容器是不同的类型。)很好,但是我将使用新的C ++语法。

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

您必须遍历向量并将每个元素推入队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM