Skip to content

Instantly share code, notes, and snippets.

@meki
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meki/f3c9e6e1ebfe22e1e1bd to your computer and use it in GitHub Desktop.
Save meki/f3c9e6e1ebfe22e1e1bd to your computer and use it in GitHub Desktop.
固定長キュークラス
#include <deque>
/**
* 固定長キュークラス
*/
template <typename T>
class FixedQueue
{
public:
typedef typename std::deque<T>::iterator iterator;
typedef typename std::deque<T>::const_iterator const_iterator;
//! サイズ指定コンストラクタ
FixedQueue(size_t size)
{
que.resize(size);
}
//! 後ろに要素を追加
void push_back(const T&& t)
{
que.pop_front();
que.push_back(t);
}
void push_back(const T& t)
{
que.pop_front();
que.push_back(t);
}
//! 前に要素を追加
void push_front(const T&& t)
{
que.pop_back();
que.push_front(t);
}
void push_front(const T& t)
{
que.pop_back();
que.push_front(t);
}
iterator begin() { return que.begin(); }
const_iterator begin() const { return que.begin(); }
iterator end() { return que.end(); }
const_iterator end() const { return que.end(); }
private:
std::deque < T > que; //!< データが格納されているコンテナ
};
#include <deque>
/**
* 固定長キュークラス
*/
template <typename T>
class FixedQueue
{
public:
typedef typename std::deque<T>::iterator iterator;
FixedQueue(size_t size)
{
que.resize(size);
}
void push_back(const T&& t)
{
que.pop_front();
que.push_back(t);
}
void push_back(const T& t)
{
que.pop_front();
que.push_back(t);
}
void push_front(const T&& t)
{
que.pop_back();
que.push_front(t);
}
void push_front(const T& t)
{
que.pop_back();
que.push_front(t);
}
iterator begin()
{
return que.begin();
}
iterator end()
{
return que.end();
}
private:
std::deque < T > que; //!< データが格納されているコンテナ
};
#include <iostream>
#include "FixedQueue.h"
using namespace std;
template <typename T>
void printQue(const FixedQueue<T>& que)
{
cout << "print: ";
for(const T& t : que)
{
cout << t << ", ";
}
cout << endl;
}
int main(void){
FixedQueue<int> que(5);
for(int i = 1; i < 100; ++i)
{
que.push_back(i);
printQue(que);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment