C++ Stack 库 - emplace() 函数
描述
C++ 函数 std::stack::emplace() 在栈顶构造并插入新元素。 新元素被插入到一个地方,即不执行复制或移动操作。
声明
以下是 std::stack::emplace() 函数形式 std::stack 头的声明。
C++11
template <class... Args> void emplace (Args&&... args);
参数
args − 转发参数以构造新元素
返回值
None
异常
取决于底层容器。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::stack::emplace() 函数的用法。
#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;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Stack contents are 5 4 3 2 1

