c++ 将一个std::queue里的内容拷贝到另一个不为空的std::queue中,最终使两个std::queue里的元素相同
时间: 2023-12-13 15:04:55 浏览: 198
您可以使用循环和std::queue的成员函数push()来实现这个功能。以下是一个示例代码:
```cpp
#include <iostream>
#include <queue>
int main() {
std::queue<int> sourceQueue;
std::queue<int> destinationQueue;
// 向sourceQueue中添加一些元素
sourceQueue.push(1);
sourceQueue.push(2);
sourceQueue.push(3);
// 将sourceQueue的元素拷贝到destinationQueue
while (!sourceQueue.empty()) {
int element = sourceQueue.front();
sourceQueue.pop();
destinationQueue.push(element);
}
// 打印destinationQueue的元素
while (!destinationQueue.empty()) {
std::cout << destinationQueue.front() << " ";
destinationQueue.pop();
}
return 0;
}
```
运行这段代码,您会发现destinationQueue中的元素与sourceQueue中的元素相同。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)