C ++ベクトルオブジェクト追加メソッド

1行動

1.オブジェクトがベクターに追加されるたびに、コピーコンストラクターを呼び出すラウンドが実行されて新しいオブジェクトが作成されます
。2。オブジェクトが追加されたときに容量が不十分な場合
(1)最初にコピー構築によって新しいオブジェクトを作成します。
(2)元のオブジェクトの拡張と移行を実行します。 ;
(3)手順(1)で作成した新しいオブジェクトを追加します。3
ベクトルの初期化時に容量が指定されていない場合、オブジェクトが追加されるたびに、容量と要素の数が0から1ずつ増加します。

2ベクトル追加オブジェクトメソッド1

2.1例

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class A
{
    
    
private:
	int value;
public:
	A() {
    
     value = 0;cout << "A() this= " << this << ",value="<<value<<endl; }
	A(const A& a) {
    
    
		cout << "A(A&) this= " << this  << endl;
		this->value = a.value;
	}
	void setValue(int value) {
    
     this->value = value; }
	int getValue() {
    
     return this->value; }
};

int main()
{
    
     
	vector<A>  v;
	cout << endl << "start create object of A:" << endl;
	A a1, a2, a3;
	a1.setValue(1);
	a2.setValue(2);
	a3.setValue(3);
	cout << endl << "start push:" << endl;
	cout << endl << "start push a1:" << endl;
	v.push_back(a1);
	cout << endl << "start push a2:" << endl;
	v.push_back(a2);
	cout << endl << "start push a3:" << endl;
	v.push_back(a3);
	cout << endl<<"start setValue:" << endl;
	a1.setValue(4);
	a2.setValue(5);
	a3.setValue(6);

	cout << endl << "start for:" << endl;
	for (auto & a : v)
	{
    
    
		cout << &a << ",value="<<a.getValue()<<endl;
	}
}

2.2操作

start create object of A:
A() this= 00AFFDE8,value=0
A() this= 00AFFDDC,value=0
A() this= 00AFFDD0,value=0

start push:

start push a1:
A(A&) this= 00C160E8

start push a2:
A(A&) this= 00C1B46C
A(A&) this= 00C1B468

start push a3:
A(A&) this= 00C1B4A8
A(A&) this= 00C1B4A0
A(A&) this= 00C1B4A4

start setValue:

start for:
00C1B4A0,value=1
00C1B4A4,value=2
00C1B4A8,value=3

備考:
Vectorの初期化後
ここに写真の説明を挿入
、a1の
ここに写真の説明を挿入
追加、a2の
ここに写真の説明を挿入
追加、a3の追加
ここに写真の説明を挿入

3ベクトル追加オブジェクトメソッド2(指定容量)

3.1例

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class A
{
    
    
private:
	int value;
public:
	A() {
    
     value = 0;cout << "A() this= " << this << ",value="<<value<<endl; }
	A(const A& a) {
    
    
		cout << "A(A&) this= " << this  << endl;
		this->value = a.value;
	}
	void setValue(int value) {
    
     this->value = value; }
	int getValue() {
    
     return this->value; }
};

int main()
{
    
     
	vector<A>  v;
	v.reserve(5);
	cout << endl << "start create object of A:" << endl;
	A a1, a2, a3;
	a1.setValue(1);
	a2.setValue(2);
	a3.setValue(3);
	cout << endl << "start push:" << endl;
	cout << endl << "start push a1:" << endl;
	v.push_back(a1);
	cout << endl << "start push a2:" << endl;
	v.push_back(a2);
	cout << endl << "start push a3:" << endl;
	v.push_back(a3);
	cout << endl<<"start setValue:" << endl;
	a1.setValue(4);
	a2.setValue(5);
	a3.setValue(6);

	cout << endl << "start for:" << endl;
	for (auto & a : v)
	{
    
    
		cout << &a << ",value="<<a.getValue()<<endl;
	}
}

3.2操作

start create object of A:
A() this= 00EFF840,value=0
A() this= 00EFF834,value=0
A() this= 00EFF828,value=0

start push:

start push a1:
A(A&) this= 011B60E8

start push a2:
A(A&) this= 011B60EC

start push a3:
A(A&) this= 011B60F0

start setValue:

start for:
011B60E8,value=1
011B60EC,value=2
011B60F0,value=3

おすすめ

転載: blog.csdn.net/skytering/article/details/105912094