[C ++プログラミングの改善] 3.2ベクトルコンテナ

3.2ベクトルコンテナ

3.2.1ベクトルの基本概念

特徴:

  • ベクトルデータ構造シングルエンド配列とも呼ばれる配列と非常によく似ています。

ベクトルと通常の配列の違い:

  • 違いは、配列が静的空間であるのに対し、ベクトルは動的に拡張できることです。

動的拡張:

  • 元のスペースの後に新しいスペースを接続するのではなく、より大きなメモリスペースを見つけて、元のデータを新しいスペースにコピーして元のスペースを解放することです。

[外部リンク画像の転送に失敗しました。元のサイトにヒル防止リンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-WNJcgE4S-1615014786757)(assets / clip_image002.jpg)]

  • ベクトルコンテナのイテレータは、ランダムアクセスをサポートするイテレータです。

3.2.2ベクトルコンストラクター

機能の説明:

  • ベクトルコンテナを作成する

関数プロトタイプ:

  • vector<T> v; //テンプレート実装クラス実装、デフォルトコンストラクタを使用
  • vector(v.begin(), v.end()); // v [begin()、end())の範囲の要素をそれ自体にコピーします。
  • vector(n, elem); //コンストラクターはn要素をそれ自体にコピーします。
  • vector(const vector &vec); //コンストラクタをコピーします。

例:

#include <vector>

void printVector(vector<int>& v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> v1; //无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int> v2(v1.begin(), v1.end());
	printVector(v2);

	vector<int> v3(10, 100);
	printVector(v3);
	
	vector<int> v4(v3);
	printVector(v4);
}

int main() {

	test01();

	system("pause");

	return 0;
}

**概要:**ベクトルの複数の構築方法は比較できません。柔軟に使用するだけです。

3.2.3ベクトル代入演算

機能の説明:

  • ベクトルコンテナに値を割り当てます

関数プロトタイプ:

  • vector& operator=(const vector &vec);//等号演算子をオーバーロードします

  • assign(beg, end); //間隔[beg、end)のデータのコピーをそれ自体に割り当てます。

  • assign(n, elem); // elemのn個のコピーをそれ自体に割り当てます。

例:

#include <vector>

void printVector(vector<int>& v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//赋值操作
void test01()
{
	vector<int> v1; //无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2;
	v2 = v1;
	printVector(v2);

	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	vector<int>v4;
	v4.assign(10, 100);
	printVector(v4);
}

int main() {

	test01();

	system("pause");

	return 0;
}

概要:ベクトル代入法は比較的単純です。operator=またはassignを使用してください。

3.2.4ベクトルの容量とサイズ

機能の説明:

  • ベクトルコンテナの容量とサイズに関する操作

関数プロトタイプ:

  • empty(); //コンテナが空かどうかを判断します

  • capacity(); //コンテナの容量

  • size(); //コンテナ内の要素の数を返します

  • resize(int num); //コンテナの長さをnumとして再指定します。コンテナが長くなると、新しい位置にデフォルト値が入力されます。

    //コンテナが短くなると、コンテナの長さを超える最後の要素が削除されます。

  • resize(int num, elem); //コンテナの長さをnumとして再指定します。コンテナが長くなる場合は、新しい位置にelem値を入力します。

    //コンテナが短くなると、コンテナの長さを超える最後の要素が削除されます

例:

#include <vector>

void printVector(vector<int>& v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	if (v1.empty())
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空" << endl;
		cout << "v1的容量 = " << v1.capacity() << endl;
		cout << "v1的大小 = " << v1.size() << endl;
	}

	//resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
	v1.resize(15,10);
	printVector(v1);

	//resize 重新指定大小 ,若指定的更小,超出部分元素被删除
	v1.resize(5);
	printVector(v1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

総括する:

  • 空かどうかを判断します—空
  • 要素の数を返します—サイズ
  • リターンコンテナの容量—容量
  • サイズを再指定します—サイズ変更

3.2.5ベクターの挿入と削除

機能の説明:

  • ベクトルコンテナを挿入および削除します

関数プロトタイプ:

  • push_back(ele); //最後に要素eleを挿入します
  • pop_back(); //最後の要素を削除します
  • insert(const_iterator pos, ele); //イテレータは、要素eleを挿入する位置posを指します
  • insert(const_iterator pos, int count,ele);//イテレータは位置posを指し、カウント要素eleを挿入します
  • erase(const_iterator pos); //イテレータが指す要素を削除します
  • erase(const_iterator start, const_iterator end);//イテレータの開始と終了の間の要素を削除します
  • clear(); //コンテナ内のすべての要素を削除します

例:


#include <vector>

void printVector(vector<int>& v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//插入和删除
void test01()
{
	vector<int> v1;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	printVector(v1);
	//尾删
	v1.pop_back();
	printVector(v1);
	//插入
	v1.insert(v1.begin(), 100);
	printVector(v1);

	v1.insert(v1.begin(), 2, 1000);
	printVector(v1);

	//删除
	v1.erase(v1.begin());
	printVector(v1);

	//清空
	v1.erase(v1.begin(), v1.end());
	v1.clear();
	printVector(v1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

総括する:

  • エンドプラグ— push_back
  • テール削除— pop_back
  • 挿入—挿入(位置イテレータ)
  • 削除—消去(位置イテレータ)
  • クリア—クリア

3.2.6ベクターデータアクセス

機能の説明:

  • ベクトル内のデータに対するアクセス操作

関数プロトタイプ:

  • at(int idx); //インデックスidxが指すデータを返します
  • operator[]; //インデックスidxが指すデータを返します
  • front(); //コンテナの最初のデータ要素を返します
  • back(); //コンテナ内の最後のデータ要素に戻ります

例:

#include <vector>

void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;

	cout << "v1的第一个元素为: " << v1.front() << endl;
	cout << "v1的最后一个元素为: " << v1.back() << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

総括する:

  • イテレータを使用してベクトルコンテナ内の要素を取得することに加えて、[]およびatも使用できます。
  • frontはコンテナの最初の要素を返します
  • backはコンテナの最後の要素を返します

3.2.7ベクトル交換コンテナ

機能の説明:

  • 2つのコンテナ内の要素の交換を実現します

関数プロトタイプ:

  • swap(vec); // vecを独自の要素と交換します

例:

#include <vector>

void printVector(vector<int>& v) {

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	printVector(v2);

	//互换容器
	cout << "互换后" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}

void test02()
{
	vector<int> v;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
	}

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	v.resize(3);

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	//收缩内存
	vector<int>(v).swap(v); //匿名对象

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;
}

int main() {

	test01();

	test02();

	system("pause");

	return 0;
}

要約:スワップは2つのコンテナーを交換でき、実用的なメモリ縮小効果を実現できます

3.2.8ベクトル予約スペース

機能の説明:

  • 容量を動的に拡張する場合、ベクトルの拡張数を減らします

関数プロトタイプ:

  • reserve(int len);//コンテナはlen要素の長さを予約し、予約された位置は初期化されず、要素にアクセスできません。

例:

#include <vector>

void test01()
{
	vector<int> v;

	//预留空间
	v.reserve(100000);

	int num = 0;
	int* p = NULL;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
		if (p != &v[0]) {
			p = &v[0];
			num++;
		}
	}

	cout << "num:" << num << endl;
}

int main() {

	test01();
    
	system("pause");

	return 0;
}

概要:データ量が多い場合は、予約を使用して最初にスペースを予約できます

詳細については、公式アカウントに従ってください。
img

おすすめ

転載: blog.csdn.net/yegeli/article/details/114442602