このクラスを使用してQVector::remove
パフォーマンスをチェックできます。
#include <QDebug>
class Dummy
{
public:
Dummy()
{
qDebug() << "dummy ctor" << ++c1;
}
Dummy(const Dummy &d)
{
qDebug() << "dummy copy ctor" << ++c2;
}
Dummy &operator=(const Dummy &d)
{
qDebug() << "dummy asign" << ++c3;
}
static int c1, c2, c3;
};
int Dummy::c1 = 0;
int Dummy::c2 = 0;
int Dummy::c3 = 0;
テスト自体:
int main(int argc, char *argv[])
{
QVector<Dummy> v;
for (int i = 0; i < 100; ++i)
{
v.append(Dummy());
}
qDebug() << "adding finished";
v.remove(0);
v.remove(v.size() - 1);
qDebug() << "end";
return 0;
}
この例Dummy
では、最初の要素を削除するときに assign 演算子が 99 回呼び出され、最後の要素を削除するとまったく呼び出されません。
とにかく、コンテナの生データにアクセスする必要がある場合は、設計上の問題があると思います。JKSH がコメントで述べたように、データ要素にメモリを動的に割り当ててから、それらのポインターをコンテナーに入れることができます。