5.vector容器的使用

vector容器

1.构造函数

/*1.默认构造-无参构造*/
/*2.通过区间的方式进行构造*/
/*3.n个elem方式构造*/
/*4.拷贝构造*/
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


void test01()
{
	/*1.默认构造-无参构造*/
	vector<int>v1;

	/*尾插*/
	for (int i = 0; i < 5 ; i++)
	{
		v1.push_back(i);
	}

	cout << "v1容器的数据: ";
	/*打印*/
	printVector(v1);

	/*2.通过区间的方式进行构造*/
	vector<int>v2(v1.begin(), v1.end());

	cout << "v2容器的数据: ";
	/*打印*/
	printVector(v2);

	/*3.n个elem方式构造*/
	vector<int>v3(5, 100);

	cout << "v3容器的数据: ";
	/*打印*/
	printVector(v3);

	/*4.拷贝构造*/
	vector<int>v4(v3);

	cout << "v4容器的数据: ";
	/*打印*/
	printVector(v4);

	return;
}


int main()
{
	test01();


	return 0;
}
运行结果

在这里插入图片描述

2.赋值

/* 1.赋值  operator= */
/* 2.赋值  assign 迭代器区间*/
/* 3.赋值  assign n个elem的方式*/
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;


void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	return;
}

void test()
{
	vector<int>v1;

	/*尾插*/
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(i);
	}
	cout << "v1容器的数据: ";

	printVector(v1);

	/* 1.赋值  operator= */
	vector<int>v2;
	v2 = v1;

	cout << "v2容器的数据: ";

	printVector(v2);

	/* 2.赋值  assign 迭代器区间*/
	vector<int>v3;
	v3.assign(v2.begin(), v2.end());/*注意是:闭开区间*/

	cout << "v3容器的数据: ";

	printVector(v3);

	/* 3.赋值  assign n个elem的方式*/
	vector<int>v4;
	v4.assign(5, 200);

	cout << "v4容器的数据: ";

	printVector(v4);

	return;
}


int main()
{
	test();

	return 0;
}
运行结果

在这里插入图片描述

3.容量和大小

/*1.empty() 如果为不空,返回值是0*/
/*2.capacity() 查询容量*/
/*3.size() 查询容器中的数据个数*/
/*4.resize() 重新指定大小*/
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	return;
}

void test()
{
	vector<int>v1;

	/*尾插*/
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "v1容器的数据: ";

	printVector(v1);

	/*1.empty() 如果为不空,返回值是0*/
	if (v1.empty())
	{
		cout << "v1容器为空" << endl;
		return ;
	}

	/*2.capacity() 查询容量*/
	cout << "v1的容量为:" << v1.capacity() << endl;

	/*3.size() 查询容器中的数据个数*/
	cout << "v1的大小为:" << v1.size() << endl;

	/*4.resize() 重新指定大小*/
	
	cout << "v1重新指定长度为15时 :";
	v1.resize(15);/*如果重新指定的比原来长了,默认用0填充新的位置*/
	//v1.resize(15, 100);/*利用重载版本,可以指定默认值的填充*/

	printVector(v1);

	cout << "v1重新指定长度为5时 :";
	v1.resize(5);/*如果重新指定的比原来短了,超出的部分会删除掉*/

	printVector(v1);

	return;
}


int main()
{
	test();

	return 0;
}
运行结果

在这里插入图片描述

4.插入和删除

/*1.尾删*/
/*2.插入 - 迭代器输入*/
/*3.删除 - 迭代器输入*/
/*4.清空*/
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;

/*1.尾删*/
/*2.插入 - 迭代器输入*/
/*3.删除 - 迭代器输入*/
/*4.清空*/
void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test()
{
	vector<int>v1;
	/*尾插*/
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	cout << "v1容器的数据: ";

	printVector(v1);

	/*1.尾删*/
	v1.pop_back();

	cout << "尾删后v1容器中的数据: ";

	printVector(v1);

	/*2.插入- 迭代器输入*/
	v1.insert(v1.begin(), 1000);

	cout << "插入后v1容器中的数据: ";

	printVector(v1);

	/*利用重载的版本,可以插入多个重复的数据*/

	v1.insert(v1.begin(), 2, 2000);

	cout << "插入后v1容器中的数据: ";

	printVector(v1);

	/*3.删除 - 迭代器输入*/
	v1.erase(v1.begin());

	cout << "删除后v1容器中的数据: ";

	printVector(v1);

	/*删除 - 区间删除*/
	v1.erase(v1.begin(), v1.end());

	if (v1.empty())/*v1为空,empty()返回值为1*/
	{
		cout << "当前容器为空!" << endl;
	}

	/*尾插*/
	v1.push_back(10);
	v1.push_back(20);

	printVector(v1);

	/*4.清空*/
	v1.clear();

	cout << "清空v1容器中的数据: ";

	if (v1.empty())/*v1为空,empty()返回值为1*/
	{
		cout << "当前容器为空!" << endl;
	}

	return;
}

int main()
{
	test();

	return 0;
}
运行结果

在这里插入图片描述

5.数据存取

/*1.利用[]方式访问数组中的元素*/
/*2.利用at方式访问数组中的元素*/
/*3.获取第一个元素*/
/*4.获取第最后一个元素*/
工程代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;

/*1.利用[]方式访问数组中的元素*/
/*2.利用at方式访问数组中的元素*/
/*3.获取第一个元素*/
/*4.获取第最后一个元素*/

void test()
{
	vector<int>v1;
	/*尾插*/
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	cout << "利用[]访问v1容器的数据: ";

	/*1.利用[]方式访问数组中的元素*/
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	cout << "利用at访问v1容器的数据: ";

	/*2.利用at方式访问数组中的元素*/
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;

	cout << "访问v1容器的第一个数据: ";

	/*3.获取第一个元素*/
	cout << v1.front() << endl;

	cout << "访问v1容器的最后一个数据: ";

	/*4.获取第最后一个元素*/
	cout << v1.back() << endl;

	return;
}

int main()
{
	test();

	return 0;
}
运行结果

在这里插入图片描述

6.互换容器

代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test()
{
	vector<int>v1;
	/*尾插*/
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	cout << "v1容器的数据: ";

	printVector(v1);

	vector<int>v2;
	/*尾插*/
	v2.push_back(100);
	v2.push_back(200);
	v2.push_back(300);
	v2.push_back(400);
	v2.push_back(500);

	cout << "v2容器的数据: ";

	printVector(v2);

	cout << "-----------交换容器的数据后-----------" << endl;

	/*交换两个容器中的数据*/
	v1.swap(v2);

	cout << "v1容器的数据: ";

	printVector(v1);

	cout << "v2容器的数据: ";

	printVector(v2);

	return;
}

void test01()
{
	/*交换容器的实际作用:利用swap可以收缩内存空间*/
	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(5);
	cout << "重新指定v容器的大小: " << endl;

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


	cout << "收缩v容器的大小: " << endl;
	/*利用swap收缩内存空间*/
	vector<int>(v).swap(v);
	cout << "v容器的容量:" << v.capacity() << endl;
	cout << "v容器的大小:" << v.size() << endl;



	return;
}
int main()
{
	test();

	cout << endl;
	cout << "swap的实际作用" << endl;

	test01();

	return 0;
}
运行结果

在这里插入图片描述

7.预留空间

代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>

using namespace std;

void printVector(const vector<int>& v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test()
{
	vector<int>v1;
	int* p = NULL;
	int num = 0;
	/*计算出尾插十万次,容器重新开辟空间的次数*/
	for (int i = 0; i < 100000; i++)
	{
		v1.push_back(i);
		if (p != &v1[0])
		{
			num++;
			p = &v1[0];
		}
	}

	cout << "v1容器重新开辟空间的次数:" << num << endl;

	return;
}

void test01()
{
	vector<int>v1;
	int* p = NULL;
	int num = 0;

	/*预留空间 - reserve*/
	v1.reserve(100000);

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

	cout << "预留空间后v1容器重新开辟空间的次数:" << num << endl;

	return;
}

int main()
{
	test();

	cout << endl;

	test01();

	return 0;
}
运行结果

在这里插入图片描述

相关推荐

  1. 使用C++中vector容器进行数据排序

    2024-04-01 17:14:03       50 阅读
  2. Vector容器详细介绍

    2024-04-01 17:14:03       47 阅读
  3. vector容器

    2024-04-01 17:14:03       39 阅读
  4. C++ vector使用

    2024-04-01 17:14:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 17:14:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 17:14:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 17:14:03       82 阅读
  4. Python语言-面向对象

    2024-04-01 17:14:03       91 阅读

热门阅读

  1. datalist是什么,有什么作用?

    2024-04-01 17:14:03       40 阅读
  2. 深入解析Oracle数据库中的外连接 (OUTER JOIN)

    2024-04-01 17:14:03       42 阅读
  3. AMBE-2000

    2024-04-01 17:14:03       41 阅读
  4. C++ | 隐藏实现pimpl(pointer to implementation)

    2024-04-01 17:14:03       34 阅读
  5. centos7 安装es8.12.0

    2024-04-01 17:14:03       32 阅读
  6. 蓝桥杯算法记录

    2024-04-01 17:14:03       43 阅读