
- C 標準庫
- C 標準庫
- C++ 標準庫
- C++ 庫 - 首頁
- C++ 庫 - <fstream>
- C++ 庫 - <iomanip>
- C++ 庫 - <ios>
- C++ 庫 - <iosfwd>
- C++ 庫 - <iostream>
- C++ 庫 - <istream>
- C++ 庫 - <ostream>
- C++ 庫 - <sstream>
- C++ 庫 - <streambuf>
- C++ 庫 - <atomic>
- C++ 庫 - <complex>
- C++ 庫 - <exception>
- C++ 庫 - <functional>
- C++ 庫 - <limits>
- C++ 庫 - <locale>
- C++ 庫 - <memory>
- C++ 庫 - <new>
- C++ 庫 - <numeric>
- C++ 庫 - <regex>
- C++ 庫 - <stdexcept>
- C++ 庫 - <string>
- C++ 庫 - <thread>
- C++ 庫 - <tuple>
- C++ 庫 - <typeinfo>
- C++ 庫 - <utility>
- C++ 庫 - <valarray>
- C++ STL 庫
- C++ 庫 - <array>
- C++ 庫 - <bitset>
- C++ 庫 - <deque>
- C++ 庫 - <forward_list>
- C++ 庫 - <list>
- C++ 庫 - <map>
- C++ 庫 - <multimap>
- C++ 庫 - <queue>
- C++ 庫 - <priority_queue>
- C++ 庫 - <set>
- C++ 庫 - <stack>
- C++ 庫 - <unordered_map>
- C++ 庫 - <unordered_set>
- C++ 庫 - <vector>
- C++ 庫 - <algorithm>
- C++ 庫 - <iterator>
- C++ 高階庫
- C++ 庫 - <any>
- C++ 庫 - <barrier>
- C++ 庫 - <bit>
- C++ 庫 - <chrono>
- C++ 庫 - <cinttypes>
- C++ 庫 - <clocale>
- C++ 庫 - <condition_variable>
- C++ 庫 - <coroutine>
- C++ 庫 - <cstdlib>
- C++ 庫 - <cstring>
- C++ 庫 - <cuchar>
- C++ 庫 - <charconv>
- C++ 庫 - <cfenv>
- C++ 庫 - <cmath>
- C++ 庫 - <ccomplex>
- C++ 庫 - <expected>
- C++ 庫 - <format>
- C++ 庫 - <future>
- C++ 庫 - <flat_set>
- C++ 庫 - <flat_map>
- C++ 庫 - <filesystem>
- C++ 庫 - <generator>
- C++ 庫 - <initializer_list>
- C++ 庫 - <latch>
- C++ 庫 - <memory_resource>
- C++ 庫 - <mutex>
- C++ 庫 - <mdspan>
- C++ 庫 - <optional>
- C++ 庫 - <print>
- C++ 庫 - <ratio>
- C++ 庫 - <scoped_allocator>
- C++ 庫 - <semaphore>
- C++ 庫 - <source_location>
- C++ 庫 - <span>
- C++ 庫 - <spanstream>
- C++ 庫 - <stacktrace>
- C++ 庫 - <stop_token>
- C++ 庫 - <syncstream>
- C++ 庫 - <system_error>
- C++ 庫 - <string_view>
- C++ 庫 - <stdatomic>
- C++ 庫 - <variant>
- C++ STL 庫速查表
- C++ STL - 速查表
C++ Stack::swap() 函式
C++ 函式std::stack::swap()交換兩個棧的所有元素。為了使用此函式,兩個棧的資料型別必須相同,但大小可以不同(如有必要可以修改)。
swap() 函式是用於處理 C++ 中棧資料結構的簡單而高效的工具。它可以用於反轉棧中元素的順序、清除棧的內容以及對棧中的元素進行排序。
語法
以下是 std::stack::swap() 函式的語法:
void swap (stack& x) noexcept;
引數
x - 另一個相同型別的棧物件。
返回值
此方法沒有返回值。
示例 1
以下示例演示了使用不同大小的 std::stack::swap() 函式。
首先,我們建立了兩個棧 's1' 和 's2'。然後,我們使用 push() 函式將值 '1 - 6' 插入 's1',並將值 '101 - 103' 插入 's2'。之後,我們使用 swap() 函式交換 's1' 和 's2' 的元素。最後,我們使用 top() 和 pop() 函式檢索並移除兩個棧中的元素。
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; for (int i = 0; i < 5; ++i) s1.push(i + 1); for (int i = 0; i < 3; ++i) s2.push(100 + i); s1.swap(s2); cout << "Contents of stack s1 after swap operation" << endl; while (!s1.empty()) { cout << s1.top() << endl; s1.pop(); } cout << endl; cout << "Contents of stack s2 after swap operation" << endl; while (!s2.empty()) { cout << s2.top() << endl; s2.pop(); } return 0; }
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
Contents of stack s1 after swap operation 102 101 100 Contents of stack s2 after swap operation 5 4 3 2 1
示例 2
現在我們嘗試交換大小相同的兩個棧 'stack1' 和 'stack2':
#include <iostream> #include <stack> int main() { std::stack<int> stack1; std::stack<int> stack2; stack1.push(1); stack1.push(2); stack1.push(3); stack2.push(4); stack2.push(5); stack2.push(6); // Swap the two stacks stack1.swap(stack2); std::cout << "Stack 1 after swap:" << std::endl; while (!stack1.empty()) { std::cout << stack1.top() << std::endl; stack1.pop(); } std::cout << "Stack 2 after swap:" << std::endl; while (!stack2.empty()) { std::cout << stack2.top() << std::endl; stack2.pop(); } return 0; }
輸出
上述程式碼的輸出如下:
Stack 1 after swap: 6 5 4 Stack 2 after swap: 3 2 1
示例 3
在這裡,我們使用 char 資料型別上的 swap() 函式來交換兩個棧 's1' 和 's2' 的元素:
#include <iostream> #include <stack> using namespace std; int main(void) { stack<char> s1; stack<char> s2; s1.push('c'); s1.push('a'); s1.push('r'); s2.push('b'); s2.push('i'); s2.push('k'); s2.push('e'); s1.swap(s2); cout << "Contents of stack s1 after swap operation" << endl; while (!s1.empty()) { cout << s1.top() << endl; s1.pop(); } cout << endl; cout << "Contents of stack s2 after swap operation" << endl; while (!s2.empty()) { cout << s2.top() << endl; s2.pop(); } return 0; }
輸出
如果我們執行上述程式碼,它將生成以下輸出:
Contents of stack s1 after swap operation e k i b Contents of stack s2 after swap operation r a c
示例 4
現在,我們嘗試將包含 2 個元素的棧 'stack1' 與空棧 'stack2' 交換:
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> stack1; stack<int> stack2; stack1.push(1); stack1.push(2); stack1.swap(stack2); cout << "Contents of stack1 after swap operation:" << endl; while (!stack1.empty()) { cout << stack1.top() << endl; stack1.pop(); } cout << endl; cout << "Contents of stack2 after swap operation:" << endl; while (!stack2.empty()) { cout << stack2.top() << endl; stack2.pop(); } return 0; }
輸出
以下是上述程式碼的輸出:
Contents of stack1 after swap operation: Contents of stack2 after swap operation: 2 1
廣告