Skip to content

Instantly share code, notes, and snippets.

@dangkhoasdc
Created February 13, 2023 01:51
Show Gist options
  • Save dangkhoasdc/bad3d4201603580b1ad0d95ba4542fde to your computer and use it in GitHub Desktop.
Save dangkhoasdc/bad3d4201603580b1ad0d95ba4542fde to your computer and use it in GitHub Desktop.
C++ Create fixed size queue
// credit: https://stackoverflow.com/a/56334648
#include <queue>
#include <deque>
#include <iostream>
template <typename T, int MaxLen, typename Container=std::deque<T>>
class FixedQueue : public std::queue<T, Container> {
public:
void push(const T& value) {
if (this->size() == MaxLen) {
this->c.pop_front();
}
std::queue<T, Container>::push(value);
}
};
int main() {
FixedQueue<int, 3> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
q.push(6);
q.push(7);
while (q.size() > 0)
{
std::cout << q.front() << std::endl;
q.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment