Last active
August 29, 2015 14:19
-
-
Save meki/f3c9e6e1ebfe22e1e1bd to your computer and use it in GitHub Desktop.
固定長キュークラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; //!< データが格納されているコンテナ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; //!< データが格納されているコンテナ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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