我有點困惑與矢量push_back行爲的方式,以下代碼片段我期望複製構造函數被調用只有兩次,但輸出建議否則。這是一種導致這種行爲的矢量內部重構嗎?不止一次向量push_back調用copy_constructor?
輸出:
Inside default Inside copy with my_int = 0 Inside copy with my_int = 0 Inside copy with my_int = 1
class Myint
{
private:
int my_int;
public:
Myint() : my_int(0)
{
cout << "Inside default " << endl;
}
Myint(const Myint& x) : my_int(x.my_int)
{
cout << "Inside copy with my_int = " << x.my_int << endl;
}
void set(const int &x)
{
my_int = x;
}
}
vector<Myint> myints;
Myint x;
myints.push_back(x);
x.set(1);
myints.push_back(x);
「emplace_back」的相關問題:http://stackoverflow.com/questions/23948442/emplace-back-does-not-behave-as-expected/23948499?s=8|0.0000#23948499 – juanchopanza 2014-11-04 17:25:12