简体   繁体   English

C ++:将元素从队列的前面移到后面

[英]C++: Moving an element from Front of queue To Back

I'm attempting to write a function called move_to_rear that moves the element at the front of the queue to the back of the queue, then making the element second in line the front element. 我正在尝试编写一个名为move_to_rear的函数,该函数将队列前面的元素移到队列的后面,然后使该元素排在前面元素的第二行。 I was able to do this using a couple different methods, but my classmate told me he did it by using just push, front, and pop. 我可以使用几种不同的方法来做到这一点,但是我的同学告诉我他只是通过使用推,向前和弹出来做到这一点。

How did he accomplish such a thing using just those three functions? 仅使用这三个功能,他是如何完成这样的事情的?

Maybe I'm missing something but since its a queue and not a stack the answer is simple: 也许我错过了一些东西,但是由于它是一个队列而不是一个堆栈,所以答案很简单:

const Item first = q.front(); //getting the first
q.pop(); //removing him
q.push(first); //adding him back to the queue (which will be in the rear)

Just to make sure you understand: 只是为了确保您了解:
Queue pops the first item in the queue, and pushes items into the back. 队列会弹出队列中的第一个项目,然后将项目推回后面。 FIFO = First In First Out. FIFO =先进先出。 Stack pops the last item in the list, and pushes items into the back too. 堆栈弹出列表中的最后一个项目,并将项目也推到后面。 LIFO = Last In First Out. LIFO =后进先出。 value of first must be const because front return reference and may be do not push when scopes are closed. first值必须为const,因为front返回引用,并且在关闭范围时可能不会推送。

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

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