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
廣告