// queueクラスの使用例 #include < iostream> #include < queue> #include < string> using namespace std; int main() { queue< string> q; cout << "Pushing one two three four\n\n"; q.push("one"); q.push("two"); q.push("three"); q.push("four"); while(!q.empty()) { cout << "Popping "; cout << q.front() << "\n"; q.pop(); } return 0; }
// キューを回転する #include < iostream> #include < queue> #include < string> using namespace std; int main() { queue< string> q; q.push("one"); q.push("two"); q.push("three"); q.push("four"); cout << "Contents of queue: "; for(int i=0; i< q.size(); i++) { cout << q.front() << " "; // 先頭要素を削除し、末尾に追加する q.push(q.front()); q.pop(); } cout << "\n\n"; cout << "Now, remove elements:\n"; while(!q.empty()) { cout << "Popping "; cout << q.front() << "\n"; q.pop(); } return 0; }
// priority_queueの使用例 #include < iostream> #include < queue> using namespace std; int main() { priority_queue< int> q; q.push(1); q.push(3); q.push(4); q.push(2); while(!q.empty()) { cout << "Popping "; cout << q.top() << "\n"; q.pop(); } return 0; }
// 異なる比較関数を使用する #include < iostream> #include < queue> #include < functional> using namespace std; int main() { priority_queue< int, vector< int>, greater< int> > q; q.push(1); q.push(3); q.push(4); q.push(2); while(!q.empty()) { cout << "Popping "; cout << q.top() << "\n"; q.pop(); } return 0; }
// priority_queueにクラスオブジェクトを格納する #include < iostream> #include < queue> #include < string> using namespace std; // 簡単なイベント管理クラス class event { int priority; string name; public: event() { name = ""; priority = 0; } event(string n, int p) { name = n; priority = p; } string getname() const { return name; } int getpriority() const { return priority; } }; // 優先度を決定する bool operator<(const event &a, const event &b) { return a.getpriority() < b.getpriority(); } int main() { priority_queue< event> q; q.push(event("Fire!", 10)); q.push(event("Mail Arrives", 2)); q.push(event("Phone rings", 3)); q.push(event("Knock on Door", 4)); // 優先度を表示する cout << "Priorities: "; while(!q.empty()) { cout << q.top().getname() << "\n"; cout << " "; q.pop(); } return 0; }
// 簡単なスタックの使用例 #include < iostream> #include < stack> using namespace std; int main() { stack< char> stck; stck.push('A'); stck.push('B'); stck.push('C'); stck.push('D'); while(!stck.empty()) { cout << "Popping: "; cout << stck.top() << "\n"; stck.pop(); } return 0; }
// 逆ポーランド方式の四則計算機 #include < iostream> #include < stack> #include < string> #include < cmath> using namespace std; int main() { stack< double> stck; double a, b; string s; do { cout << ": "; cin >> s; switch(s[0]) { case 'q': // 計算機を終了する break; case '.': // スタックのトップを表示する cout << stck.top() << "\n"; break; case '+': // 加算 if(stck.size() < 2) { cout << "Operand Missing\n"; break; } a = stck.top(); stck.pop(); b = stck.top(); stck.pop(); cout << a+b << "\n"; stck.push(a+b); break; case '-': // 減算 // 負の値が入力された場合 if(s.size() != 1) { // 値をスタックにプッシュする stck.push(atof(s.c_str())); break; } // そうでなければ減算を行う if(stck.size() < 2) { cout << "Operand Missing\n"; break; } a = stck.top(); stck.pop(); b = stck.top(); stck.pop(); cout << b-a << "\n"; stck.push(b-a); break; case '*': // 乗算 if(stck.size() < 2) { cout << "Operand Missing\n"; break; } a = stck.top(); stck.pop(); b = stck.top(); stck.pop(); cout << a*b << "\n"; stck.push(a*b); break; case '/': // 除算 if(stck.size() < 2) { cout << "Operand Missing\n"; break; } a = stck.top(); stck.pop(); b = stck.top(); stck.pop(); cout << b/a << "\n"; stck.push(b/a); break; default: // 値をスタックにプッシュする stck.push(atof(s.c_str())); break; } } while(s != "q"); return 0; }