C++STL stack実装(スタック)
1)Stackは
関連コンテナは、線形クラスdequeのインタフェースを簡単に修飾することによって得られる別の「コンテナクラス」であり、コンテナ(container)ではなくアダプタ(adapter)にまとめられることが多い.
stackは遍歴できないので、反復器はありません!!!
下位コンテナはdequeのほかlistも使用できます.
2)使用
ロードするヘッダファイル:#include using namespace std;
template >
3)主な方法は以下の通りである.
Empty()スタックが空の場合は真を返す
pop()スタックトップ要素を除去(スタックトップ要素の値は返さない)
push()スタックの上部に要素を追加
size()戻りスタック内の要素数
top()はスタックトップ要素を返します
4)例:
[html] view plain
copy
print ?
#include #include
using namespace std; int main()
{ stack myStack;//定義スタック myStack.push(5);//スタック myStack.push(6);
myStack.push(7); myStack.pop();//出桟 cout< cout<
cout< return 0;
}
ファイルの読み込み例
[html] view plain
copy
print ?
#include #include
#include #include
#include using namespace std;
int main(){ ifstream inf;
inf.open("temp.txt"); if (!inf) {
cerr<<"cannot open file for input!"< return EXIT_FAILURE; }
stack s; string line;
while(getline(inf,line){//1行のテキストを読み込む s.push(line);//圧入桟} inf.close(); while(!s.empty(){//スタックが空ではなく、出力スタックトップ要素 cout< s.pop();//弾桟} return 0; }
5)stackの実現
[html] view plain
copy
print ?
#include #include
#include using namespace std;
template
class stack {
private: deque elems;//実容器 public:
void push(T const&); void pop(void);
T top() const; bool empty() const
{ return elems.empty();
}
template//クラスメンバーテンプレート stack & operator= (stack const&);
//反復器を実装します.
typedef const T* const_iterator; T* end(void);
T* begin(void); };
template
template stack& stack::operator =(const stack &op2)
{ if((void*)this == (void*)&op2)
return *this; stack tmp(op2);
elems.clear();
while(!tmp.empty()) {
elems.push_front(tmp.top()); tmp.pop();
} return *this;
}
template void stack::push(T const& elem)
{ elems.push_back(elem);
}
template void stack::pop()
{ if(elems.empty())
throw out_of_range("stack<>::pop() :empty stack"); elems.pop_back();
}//反復器を実装します.
template T stack::top() const
{ if(elems.empty())
throw out_of_range("stack<>::top() :empty stack"); return elems.back();
}//最初の要素を指します.
template T* stack::begin()
{ return (&(elems.front()));
}//ここで実現するのは、未端を指す次の要素です.
template T* stack::end()
{ return ((&(elems.back()))+1);
}
int main() {
stack intStack; stack charStack;
intStack.push(10);
intStack.push(19); intStack.push(39);
cout<<"top:"<
cout<<「値を割り当てて出力:」<
charStack = intStack;
stack::const_iterator iterator; iterator = charStack.begin();
while(iterator != charStack.end()) {
cout<< *iterator< ++iterator;
} }
1)Stackは
関連コンテナは、線形クラスdequeのインタフェースを簡単に修飾することによって得られる別の「コンテナクラス」であり、コンテナ(container)ではなくアダプタ(adapter)にまとめられることが多い.
stackは遍歴できないので、反復器はありません!!!
下位コンテナはdequeのほかlistも使用できます.
2)使用
ロードするヘッダファイル:#include using namespace std;
template >
3)主な方法は以下の通りである.
Empty()スタックが空の場合は真を返す
pop()スタックトップ要素を除去(スタックトップ要素の値は返さない)
push()スタックの上部に要素を追加
size()戻りスタック内の要素数
top()はスタックトップ要素を返します
4)例:
[html] view plain
copy
print ?
#include
using namespace std;
{
myStack.push(7);
cout<
}
#include
#include
using namespace std;
int main()
{
stack myStack;//
myStack.push(5);//
myStack.push(6);
myStack.push(7);
myStack.pop(); //
cout<
ファイルの読み込み例
[html] view plain
copy
print ?
#include
#include
#include
int main(){
inf.open("temp.txt");
cerr<<"cannot open file for input!"<
stack
while(getline(inf,line){//1行のテキストを読み込む
#include
#include
#include
#include
#include
using namespace std;
int main(){
ifstream inf;
inf.open("temp.txt");
if (!inf) {
cerr< s;
string line;
while (getline(inf,line)){//
s.push(line); // }
inf.close();
while (!s.empty()){// ,
cout<
5)stackの実現
[html] view plain
copy
print ?
#include
#include
class stack
private:
void push(T const&);
T top() const;
{
}
template
typedef const T* const_iterator;
T* begin(void);
template
{
return *this;
elems.clear();
while(!tmp.empty())
elems.push_front(tmp.top());
}
}
template
{
}
template
{
throw out_of_range("stack<>::pop() :empty stack");
}
template
{
throw out_of_range("stack<>::top() :empty stack");
}
template
{
}
template
{
}
int main()
stack
intStack.push(19);
cout<<「値を割り当てて出力:」<
charStack = intStack;
stack
while(iterator != charStack.end())
cout<< *iterator<
}