我试图理解Vector C++ 中的emplace_backvs。push_back虽然emplace_back和push_back的整个过程都是在最后追加数据,但从我对emplace的理解来看,它没有创建临时对象。但是当我看到日志时,两种行为保持不变,需要帮助理解 emplace_back 与 Push_back
#include <iostream>
#include <vector>
using namespace std;
struct Foo
{
int myint;
Foo(int n)
{
cout << "constructor called " << this << endl;
myint = n;
}
Foo(const Foo& rhs )
{
cout << "copy constructor called " << this << endl;
myint = rhs.myint;
}
void display() {
cout << "in Display Foo int = " << myint << endl;
}
};
int main()
{
cout<<"********emplace example start …Run Code Online (Sandbox Code Playgroud)