
- 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++ 程式設計資源
- C++ 程式設計教程
- C++ 有用資源
- C++ 討論
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