推回之间的 C++ 差异
C++ Differences Between Pushbacks
下面是具有不同类型的回退的代码。就此而言,我的意思是用不同的输入推回。一个例子是 accounts.push_back(five)
。我不确定为什么每种推回的构造函数打印输出都不同。不仅顺序不一样,次数也不一样,比如使用的深拷贝构造函数也不一样
#include <iostream>
#include <vector>
class account
{
private:
std::string *name_ptr;
double *total_ptr;
public:
std::string get_name() { return *name_ptr; }
void set_name(std::string new_name) { *name_ptr = new_name; }
double get_total() { return *total_ptr; }
void set_total(double new_total) { *total_ptr = new_total; }
void deposit(double amount) { *total_ptr += amount; }
void withdraw(double amount) { *total_ptr -= amount; }
account(std::string name, double total = 0)
{
name_ptr = new std::string {name};
total_ptr = new double {total};
std::cout << "Constructor for " << *name_ptr << std::endl;
}
account(const account &source)
: account {*(source.name_ptr), *(source.total_ptr)}
{ std::cout << "Deep copy constructor for " << *name_ptr << std::endl; }
account(account &&source) noexcept
: name_ptr(source.name_ptr), total_ptr(source.total_ptr)
{
source.name_ptr = nullptr;
source.total_ptr = nullptr;
std::cout << "Move constructor for " << *name_ptr << std::endl;
}
~account()
{
if(name_ptr == nullptr) std::cout << "Destructor for nullptr" << std::endl;
else std::cout << "Destructor for " << *name_ptr << std::endl;
delete name_ptr;
delete total_ptr;
}
};
int main()
{
std::vector<account> accounts;
account one {"Jonathon Stevenson"};
accounts.push_back( account(one) );
account two {"Cliffard Stevenson"};
accounts.push_back( account(two) );
account three {"Tom Stevenson"};
accounts.push_back( account(three) );
std::cout << std::endl;
account four {"Jonathon Richardson"};
accounts.push_back(four);
account five {"Cliffard Richardson"};
accounts.push_back(five);
account six {"Tom Richardson"};
accounts.push_back(six);
std::cout << std::endl;
accounts.push_back( account {"Jonathon Watson"} );
accounts.push_back( account {"Cliffard Watson"} );
accounts.push_back( account {"Tom Watson"} );
std::cout << std::endl;
accounts.push_back( account("Jonathon Sullivanson") );
accounts.push_back( account("Cliffard Sullivanson") );
accounts.push_back( account("Tom Sullivanson") );
std::cout << std::endl;
return 0;
}
输出:
Constructor for Jonathon Stevenson
Constructor for Jonathon Stevenson
Deep copy constructor for Jonathon Stevenson
Move constructor for Jonathon Stevenson
Destructor for nullptr
Constructor for Cliffard Stevenson
Constructor for Cliffard Stevenson
Deep copy constructor for Cliffard Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Jonathon Stevenson
Destructor for nullptr
Destructor for nullptr
Constructor for Tom Stevenson
Constructor for Tom Stevenson
Deep copy constructor for Tom Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Jonathon Richardson
Constructor for Jonathon Richardson
Deep copy constructor for Jonathon Richardson
Constructor for Cliffard Richardson
Constructor for Cliffard Richardson
Deep copy constructor for Cliffard Richardson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Richardson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Tom Richardson
Constructor for Tom Richardson
Deep copy constructor for Tom Richardson
Constructor for Jonathon Watson
Move constructor for Jonathon Watson
Destructor for nullptr
Constructor for Cliffard Watson
Move constructor for Cliffard Watson
Destructor for nullptr
Constructor for Tom Watson
Move constructor for Tom Watson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Richardson
Move constructor for Cliffard Richardson
Move constructor for Tom Richardson
Move constructor for Jonathon Watson
Move constructor for Cliffard Watson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Jonathon Sullivanson
Move constructor for Jonathon Sullivanson
Destructor for nullptr
Constructor for Cliffard Sullivanson
Move constructor for Cliffard Sullivanson
Destructor for nullptr
Constructor for Tom Sullivanson
Move constructor for Tom Sullivanson
Destructor for nullptr
Destructor for Tom Richardson
Destructor for Cliffard Richardson
Destructor for Jonathon Richardson
Destructor for Tom Stevenson
Destructor for Cliffard Stevenson
Destructor for Jonathon Stevenson
Destructor for Jonathon Stevenson
Destructor for Cliffard Stevenson
Destructor for Tom Stevenson
Destructor for Jonathon Richardson
Destructor for Cliffard Richardson
Destructor for Tom Richardson
Destructor for Jonathon Watson
Destructor for Cliffard Watson
Destructor for Tom Watson
Destructor for Jonathon Sullivanson
Destructor for Cliffard Sullivanson
Destructor for Tom Sullivanson
这就是 std::vector
的工作原理。
每当它用完内存时,在下一个 std::vector::push_back
时,它只是分配一个新块,复制(或移动,如果可能)所有数据,然后删除旧数据。由于它无法真正知道您需要多少 space,因此自动重新分配依赖于某种策略,其中最常见的是分配 N * 2
,其中 N
是当前容量。现在让我们看看 main
.
中发生了什么
std::vector<account> accounts;
- 这将创建一个空向量(无分配)。
account one {"Jonathon Stevenson"};
- 这会通过调用您的 class 创建一个对象,让我们称之为字符串构造函数,这是您将第一个输出输出到控制台的地方。
accounts.push_back(account(one));
- 这个负责接下来的三个消息(account(one)
使用复制构造函数创建一个临时文件,它通过调用字符串构造函数工作;在那之后,临时文件被移动到向量)。这里accounts
的容量为1;
创建和推送的下一个“迭代”以相同的方式工作,除了您要进行额外的移动:即 vector
将数据移动到新位置。这里accounts
的容量是2;
下一个也一样。这里accounts
的容量是4;
然后,在 accounts.push_back(four);
- 您不再创建过多的临时文件,因此看不到移动构造函数的工作。这里accounts
的容量是4;
在下一个 push_back
发生另一个重新分配,您可以看到 vector
将所有内容(4 个元素)移动到新位置。
最后,当你尝试 accounts.push_back( account("Tom Sullivanson") );
你的容量是 8 并且 accounts
中的元素数量也是 8,所以你可以看到 vector
再次移动它的内容到一个新位置 - 恰好 8 次。现在容量为 16,此程序中不会再发生重新分配。
下面是具有不同类型的回退的代码。就此而言,我的意思是用不同的输入推回。一个例子是 accounts.push_back(five)
。我不确定为什么每种推回的构造函数打印输出都不同。不仅顺序不一样,次数也不一样,比如使用的深拷贝构造函数也不一样
#include <iostream>
#include <vector>
class account
{
private:
std::string *name_ptr;
double *total_ptr;
public:
std::string get_name() { return *name_ptr; }
void set_name(std::string new_name) { *name_ptr = new_name; }
double get_total() { return *total_ptr; }
void set_total(double new_total) { *total_ptr = new_total; }
void deposit(double amount) { *total_ptr += amount; }
void withdraw(double amount) { *total_ptr -= amount; }
account(std::string name, double total = 0)
{
name_ptr = new std::string {name};
total_ptr = new double {total};
std::cout << "Constructor for " << *name_ptr << std::endl;
}
account(const account &source)
: account {*(source.name_ptr), *(source.total_ptr)}
{ std::cout << "Deep copy constructor for " << *name_ptr << std::endl; }
account(account &&source) noexcept
: name_ptr(source.name_ptr), total_ptr(source.total_ptr)
{
source.name_ptr = nullptr;
source.total_ptr = nullptr;
std::cout << "Move constructor for " << *name_ptr << std::endl;
}
~account()
{
if(name_ptr == nullptr) std::cout << "Destructor for nullptr" << std::endl;
else std::cout << "Destructor for " << *name_ptr << std::endl;
delete name_ptr;
delete total_ptr;
}
};
int main()
{
std::vector<account> accounts;
account one {"Jonathon Stevenson"};
accounts.push_back( account(one) );
account two {"Cliffard Stevenson"};
accounts.push_back( account(two) );
account three {"Tom Stevenson"};
accounts.push_back( account(three) );
std::cout << std::endl;
account four {"Jonathon Richardson"};
accounts.push_back(four);
account five {"Cliffard Richardson"};
accounts.push_back(five);
account six {"Tom Richardson"};
accounts.push_back(six);
std::cout << std::endl;
accounts.push_back( account {"Jonathon Watson"} );
accounts.push_back( account {"Cliffard Watson"} );
accounts.push_back( account {"Tom Watson"} );
std::cout << std::endl;
accounts.push_back( account("Jonathon Sullivanson") );
accounts.push_back( account("Cliffard Sullivanson") );
accounts.push_back( account("Tom Sullivanson") );
std::cout << std::endl;
return 0;
}
输出:
Constructor for Jonathon Stevenson
Constructor for Jonathon Stevenson
Deep copy constructor for Jonathon Stevenson
Move constructor for Jonathon Stevenson
Destructor for nullptr
Constructor for Cliffard Stevenson
Constructor for Cliffard Stevenson
Deep copy constructor for Cliffard Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Jonathon Stevenson
Destructor for nullptr
Destructor for nullptr
Constructor for Tom Stevenson
Constructor for Tom Stevenson
Deep copy constructor for Tom Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Jonathon Richardson
Constructor for Jonathon Richardson
Deep copy constructor for Jonathon Richardson
Constructor for Cliffard Richardson
Constructor for Cliffard Richardson
Deep copy constructor for Cliffard Richardson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Richardson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Tom Richardson
Constructor for Tom Richardson
Deep copy constructor for Tom Richardson
Constructor for Jonathon Watson
Move constructor for Jonathon Watson
Destructor for nullptr
Constructor for Cliffard Watson
Move constructor for Cliffard Watson
Destructor for nullptr
Constructor for Tom Watson
Move constructor for Tom Watson
Move constructor for Jonathon Stevenson
Move constructor for Cliffard Stevenson
Move constructor for Tom Stevenson
Move constructor for Jonathon Richardson
Move constructor for Cliffard Richardson
Move constructor for Tom Richardson
Move constructor for Jonathon Watson
Move constructor for Cliffard Watson
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Destructor for nullptr
Constructor for Jonathon Sullivanson
Move constructor for Jonathon Sullivanson
Destructor for nullptr
Constructor for Cliffard Sullivanson
Move constructor for Cliffard Sullivanson
Destructor for nullptr
Constructor for Tom Sullivanson
Move constructor for Tom Sullivanson
Destructor for nullptr
Destructor for Tom Richardson
Destructor for Cliffard Richardson
Destructor for Jonathon Richardson
Destructor for Tom Stevenson
Destructor for Cliffard Stevenson
Destructor for Jonathon Stevenson
Destructor for Jonathon Stevenson
Destructor for Cliffard Stevenson
Destructor for Tom Stevenson
Destructor for Jonathon Richardson
Destructor for Cliffard Richardson
Destructor for Tom Richardson
Destructor for Jonathon Watson
Destructor for Cliffard Watson
Destructor for Tom Watson
Destructor for Jonathon Sullivanson
Destructor for Cliffard Sullivanson
Destructor for Tom Sullivanson
这就是 std::vector
的工作原理。
每当它用完内存时,在下一个 std::vector::push_back
时,它只是分配一个新块,复制(或移动,如果可能)所有数据,然后删除旧数据。由于它无法真正知道您需要多少 space,因此自动重新分配依赖于某种策略,其中最常见的是分配 N * 2
,其中 N
是当前容量。现在让我们看看 main
.
std::vector<account> accounts;
- 这将创建一个空向量(无分配)。
account one {"Jonathon Stevenson"};
- 这会通过调用您的 class 创建一个对象,让我们称之为字符串构造函数,这是您将第一个输出输出到控制台的地方。
accounts.push_back(account(one));
- 这个负责接下来的三个消息(account(one)
使用复制构造函数创建一个临时文件,它通过调用字符串构造函数工作;在那之后,临时文件被移动到向量)。这里accounts
的容量为1;
创建和推送的下一个“迭代”以相同的方式工作,除了您要进行额外的移动:即 vector
将数据移动到新位置。这里accounts
的容量是2;
下一个也一样。这里accounts
的容量是4;
然后,在 accounts.push_back(four);
- 您不再创建过多的临时文件,因此看不到移动构造函数的工作。这里accounts
的容量是4;
在下一个 push_back
发生另一个重新分配,您可以看到 vector
将所有内容(4 个元素)移动到新位置。
最后,当你尝试 accounts.push_back( account("Tom Sullivanson") );
你的容量是 8 并且 accounts
中的元素数量也是 8,所以你可以看到 vector
再次移动它的内容到一个新位置 - 恰好 8 次。现在容量为 16,此程序中不会再发生重新分配。