c++实现一个queue
时间: 2024-04-17 13:24:51 浏览: 8
Sure! Here's an example implementation of a queue in C++:
```cpp
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
// Enqueue elements
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
// Print the front element
std::cout << "Front element: " << myQueue.front() << std::endl;
// Print the size of the queue
std::cout << "Size of the queue: " << myQueue.size() << std::endl;
// Dequeue elements
while (!myQueue.empty()) {
std::cout << "Popped element: " << myQueue.front() << std::endl;
myQueue.pop();
}
return 0;
}
```
This code demonstrates the usage of the `<queue>` standard library in C++. It creates a queue named `myQueue` and performs various operations such as enqueueing elements using `push()`, accessing the front element with `front()`, getting the size of the queue with `size()`, and dequeuing elements using `pop()`.
Please note that this is a basic implementation, and you can extend it further based on your specific requirements.