C++ Stack::emplace() 函式



C++ 的std::stack::emplace()函式在棧頂構造並插入一個新元素。新元素是就地插入的,即不執行復制或移動操作。此函式接受單個值作為引數,該引數將被轉發以在棧中構造新元素。

成員函式emplace()允許在容器(棧)中就地構造物件,而底層容器的成員函式emplace_back()負責實際將物件新增到容器中。因此,emplace()函式有效地呼叫了帶有轉發引數的emplace_back()函式。

emplace()函式比push()函式更高效,因為它避免了建立臨時物件並將其複製或移動到棧中的開銷(使用額外記憶體)。

語法

以下是std::stack::emplace()函式的語法:

void stack_name.emplace(value);

引數

value - 它是轉發用於構造新元素的值。

返回值

此函式不返回值。

示例 1

以下示例演示了std::stack::emplace()函式的用法。首先,我們嘗試建立一個棧s。然後,使用for迴圈使用emplace()函式將五個元素插入到棧中。每個元素都是透過將值“1”與i的當前值相加建立的。最後,我們使用pop()函式彈出棧中的所有元素並將其列印到控制檯。

#include <iostream>
#include <stack>
using namespace std;

int main(void) {
   stack<int> s;
   for (int i = 0; i < 5; ++i)
      s.emplace(i + 1);
   while (!s.empty()) {
      cout << s.top() << endl;
      s.pop();
   }
   return 0;
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

5
4
3
2
1

示例 2

在這裡,我們使用emplace()函式在棧頂插入三個簡單的字串並列印它們。

#include <iostream>
#include <stack>
#include <string>

int main() {
   std::stack<std::string> myStack;
   // Use emplace() to insert elements into the stack
   myStack.emplace("first");
   myStack.emplace("second");
   myStack.emplace("third");
   // Print the elements of the stack
   std::cout << "Elements in stack are: ";
   while (!myStack.empty()) {
      std::cout << myStack.top() << std::endl;
      myStack.pop();
   }
   return 0;
}

輸出

如果我們執行上面的程式碼,它將生成以下輸出:

Elements in stack are: third
second
first

示例 3

現在,我們嘗試在棧頂插入10的乘法表,然後分別列印它以及棧中存在的元素總數。

#include <iostream>
#include <stack>
using namespace std;

int main(){
   stack<int> stck;
   int total = 0;
   stck.emplace(10);
   stck.emplace(20);
   stck.emplace(30);
   stck.emplace(40);
   stck.emplace(50);
   stck.emplace(60);
   stck.emplace(70);
   stck.emplace(80);
   stck.emplace(90);
   stck.emplace(100);
   cout << "Elements in stack are: ";
   while (!stck.empty()){
      cout<<stck.top() << " ";
      stck.pop();
      total++;
   }
   cout<<"\nTotal number of elements in stack are: "<<total;
   return 0;
}

輸出

上述程式碼的輸出如下:

Elements in stack are: 100 90 80 70 60 50 40 30 20 10 
Total number of elements in stack are: 10

示例 4

emplace()函式比push()函式更高效,因為它避免了建立臨時物件並將其複製或移動到棧中的開銷(使用額外記憶體)。

在這個例子中,我們定義了一個自定義類MyClass,它帶有一個建構函式和複製/移動建構函式,這些函式將訊息列印到控制檯。然後,我們建立了一個空的MyClass物件棧,並使用emplace()函式和push()函式將三個元素插入到棧中。

使用emplace()函式時,我們將每個元素作為引數提供給函式,我們看到每個元素都是就地建立並插入到棧中的。

使用push()函式時,我們建立MyClass型別的臨時物件,然後將其複製或移動到棧中。這需要額外的記憶體來儲存臨時物件,並且可能會導致複製或移動物件到棧中的額外開銷。

#include <iostream>
#include <stack>
#include <string>

class MyClass {
   public:
      MyClass(const std::string& str) : mStr(str) {
         std::cout << "Constructing MyClass with string: " << mStr << std::endl;
      }
      MyClass(const MyClass& other) : mStr(other.mStr) {
         std::cout << "Copying MyClass with string: " << mStr << std::endl;
      }
      MyClass(MyClass&& other) : mStr(std::move(other.mStr)) {
         std::cout << "Moving MyClass with string: " << mStr << std::endl;
      }
   private:
      std::string mStr;
};
int main() {
   std::stack<MyClass> myStack;
   std::cout << "Using emplace()" << std::endl;
   myStack.emplace("first");
   myStack.emplace("second");
   myStack.emplace("third");
   std::cout << "Using push()" << std::endl;
   MyClass myClass1("first");
   MyClass myClass2("second");
   MyClass myClass3("third");
   myStack.push(myClass1);
   myStack.push(myClass2);
   myStack.push(myClass3);
   return 0;
}

輸出

執行上述程式碼時,我們將獲得以下輸出:

Using emplace()
Constructing MyClass with string: first
Constructing MyClass with string: second
Constructing MyClass with string: third
Using push()
Constructing MyClass with string: first
Constructing MyClass with string: second
Constructing MyClass with string: third
Copying MyClass with string: first
Copying MyClass with string: second
Copying MyClass with string: third
廣告