我创建了一个类,用于跟踪具有唯一ID的实例。构造此类的新实例(或构造它的副本)提供了一个唯一的ID,该ID来自ID池。销毁会将ID放回池中。打印语句观察实例的创建和销毁时间。
#include <iostream>
#include <vector>
#include <list>
class IdPool {
public:
IdPool() {
m_id = allocateID();
std::cout << "c'tor id: " << m_id << std::endl;
}
~IdPool() {
freeID(m_id);
std::cout << "d'tor free id: " << m_id << std::endl;
}
IdPool(const IdPool& obj) {
m_id = allocateID();
std::cout << "copy c'tor id: " << m_id << std::endl;
}
class Init {
public:
Init(const int maxIDs) {
for (int i=maxIDs; i>=1; --i) {
s_idArray.push_back(i);
};
}
};
int id() { return m_id; }
private:
int allocateID() {
if (s_idArray.empty())
return 0;
else {
int id = s_idArray.back();
s_idArray.pop_back();
return id;
}
}
bool freeID(int id) {
if ( (id > 0 ) && (s_idArray.size() < s_maxIdCount) ) {
s_idArray.push_back(id);
return true;
} else {
return false;
}
}
static std::vector<int> s_idArray;
static const size_t s_maxIdCount;
static Init s_setIdCount;
int m_id;
};
const size_t IdPool::s_maxIdCount = 10;
std::vector<int> IdPool::s_idArray;
IdPool::Init IdPool::s_setIdCount(IdPool::s_maxIdCount);
int main(int argc, char* argv[]) {
using namespace std;
cout << endl << "-- push 2 IDs to list --" << endl;
list<IdPool> listId;
for (int i = 0; i < 2; ++i) {
listId.push_back(IdPool());
cout << "push_back to list id: " << listId.back().id() << endl << endl;
}
cout << endl << "-- push 2 IDs to vector --" << endl;
vector<IdPool> vecId;
for (int i = 0; i < 2; ++i) {
vecId.push_back(IdPool());
cout << "push_back to vector id: " << vecId.back().id() << endl << endl;
}
cout << endl << "-- push 2 IDs to preallocated vector --" << endl;
vector<IdPool> vecIdReserved;
vecIdReserved.reserve(5);
for (int i = 0; i < 2; ++i) {
vecIdReserved.push_back(IdPool());
cout << "push_back to reserved vector id: " << vecIdReserved.back().id() << endl << endl;
}
return 0;
}
测试ID生成器类时,我观察到以下行为:
[列表:将新ID推送到列表中,可以按预期工作(将临时对象的新ID拉出,在临时对象被销毁后放回去,请参见输出。
向量:将新的ID推到向量上会创建许多临时对象,对应于向量的大小。在每次push_back操作之后,向量的尾部都包含相同的ID,请参见下面的输出。如果我想使用IdPool类标识派生类的实例,则这不是理想的行为。我希望每个push_back操作只有一个临时表。我在这里想念什么?
编辑:推送到预分配的向量与列表相同。在这种情况下,这可能是解决方法。我只需要记住在使用向量之前先保留即可。
输出
-- push 2 IDs to list --
c'tor id: 1
copy c'tor id: 2
d'tor free id: 1
push_back to list id: 2
c'tor id: 1
copy c'tor id: 3
d'tor free id: 1
push_back to list id: 3
-- push 2 IDs to vector --
c'tor id: 1
copy c'tor id: 4
d'tor free id: 1
push_back to vector id: 4
c'tor id: 1
copy c'tor id: 5
d'tor free id: 4
copy c'tor id: 4
d'tor free id: 1
push_back to vector id: 4
-- push 2 IDs to preallocated vector --
c'tor id: 1
copy c'tor id: 6
d'tor free id: 1
push_back to reserved vector id: 6
c'tor id: 1
copy c'tor id: 7
d'tor free id: 1
push_back to reserved vector id: 7