vector容器(单端数组)

1、vector容器基本概念。
1)功能。
vector容器数据结构与数组非常相似,也称为单端数组。

2)vector与普通数组区别?
普通数组    --> 静态空间
vector容器  --> 动态扩展

3)动态扩展。
并不是在原空间之后继续接新空间,而是找更大的内存空间,然后原数据拷贝到新空间上,释放原空间。

4)迭代器。
vector容器迭代器就是支持随机访问的迭代器。

5)头文件 #include <vector>  string类包含在#include <iostream>中

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

void printVector(vector<int> &v)//引用做参数
{
    //vector<int>::iterator it;迭代器 v.begin(); 容器第一个 v.end(); 容器最后一个的下一个
	for(vector<int>::iterator it = v.begin() ;it != v.end() ;it++)
	{
		cout << *it << "  ";    //输出数据后输出空格
	}
	cout << endl;       //换行
}

int main(int argc,char *argv[])
{
	vector<int> v1;   //默认构造
	v1.push_back(10); //尾插插入元素
	v1.push_back(20);
	v1.push_back(30);

    //构造函数
	printVector(v1);  //10  20  30 

    //vector(v.begin(),v.end()); 
	vector<int> v2(v1.begin(),v1.end());//将v[begin(),end()]区间中的元素拷贝给本身
	printVector(v2);  //10  20  30 

	vector<int> v3(v2); //拷贝构造函数
	printVector(v3);  //10  20  30 

    //赋值操作
	vector<int> v4;
	v4 = v3;          //运算符"="重载
	printVector(v4);  //10  20  30 

	vector<int> v5;
    //assign(beg,end);
	v5.assign(v4.begin(),v4.end());//将(beg,end)区间中的数据拷贝赋值给本身
	printVector(v5);  //10  20  30 

    //容量和大小
	if(v5.empty())  //判断容器是否为空
	{
		cout << "v5容器为空" << endl;
	}
	else{
		cout << "v5容器不为空" << endl;                 //v5容器不为空
		cout << "v5的容量:" << v5.capacity() << endl;   //v5的容量:3
		cout << "v5的大小:" << v5.size() << endl;       //v5的大小:3
	}

	/*resize(int num,elem);重新指定容器的长度为num,若容器变长,则以elem值填充新位置,
    如果容器变短,则末尾超出容器的长度的元素被删除。*/
    v5.resize(15,6);
	printVector(v5);      //10  20  30  6  6  6  6  6  6  6  6  6  6  6  6
    //删除
	v5.pop_back();        //删除最后一个元素
	printVector(v5);      //10  20  30  6  6  6  6  6  6  6  6  6  6  6

	v5.insert(v5.begin(),100);//迭代器指向位置v5.begin()插入元素100
	printVector(v5);      //100  10  20  30  6  6  6  6  6  6  6  6  6  6  6

	v5.erase(v5.begin()); //删除迭代器指向的元素
	printVector(v5);      //10  20  30  6  6  6  6  6  6  6  6  6  6  6

	//v5.clear();         //删除容器中所有的元素
	//printVector(v5);    //cout << endl;

    //数据存取
	cout << v5.at(1) << endl; //at(int idx); 返回索引idx所指的数据
	cout << v5[1] << endl;    //20
    //返回容器中第一个数据元素
	cout << v5.front() << endl; //10
    //返回容器中最后一个数据元素
	cout << v5.back() << endl;  //6

	cout << "-----------------" << endl;//-----------------
	printVector(v1);            //10  20  30
	printVector(v5);            //10  20  30  6  6  6  6  6  6  6  6  6  6  6
    //互换容器
	v5.swap(v1);                //swap(vec); //将vec与本身的元素互换
	printVector(v1);            //10  20  30  6  6  6  6  6  6  6  6  6  6  6
	printVector(v5);            //10  20  30

    //预留空间
	vector<int> v6;
	//v6.reserve(100000);//reserve(int len); 容器预留len个元素长度,
                       //预留位置不初始化,元素不可访问
	int *p = NULL;

	int i;
	int num = 0;
	for(i=0;i<100000;i++)
	{
		v6.push_back(i);    //尾插

		//如果不等于,说明扩容了一次。
		if(p != &v6[0])
		{
			p = &v6[0];
			num++;
		}
	}
    //p是空指针,刚进来if会判断为真
	cout << "num = " << num << endl; //不加v6.reserve(100000);:num = 18 加了:num = 1

	return 0;
}
  1. 构造函数

vector<T> v;        //采用模板实现类实现,默认构造函数。

vector(v.begin(),v.end()); //将v[begin(),end()]区间中的元素拷贝给本身。

vector(n,elem);            //构造函数将n个elem拷贝给本身。

vector(const vector &vec); //拷贝构造函数

赋值操作

vector& operator=(const vector &vec); //重载等号操作符

assign(beg,end); //将(beg,end)区间中的数据拷贝赋值给本身。

assign(n,elem);  //将n个elem拷贝赋值给本身。

容量和大小

empty();             //判断容器是否为空

capacity();          //容器的容量

size();              //返回容器中元素的个数

resize(int num);     //重新指定容器的长度为num,若容器变长,则以默认值填充新位置,如果容器变短,则末尾超出容器的长度的元素被删除。

resize(int num,elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器的长度的元素被删除。

插入和删除

push_back(ele);                           //尾插插入元素

pop_back();                               //删除最后一个元素

insert(const_iterator pos,ele);           //迭代器指向位置pos插入元素ele。

insert(const_iterator pos,int count,ele); //迭代器指向位置pos插入count个元素ele

erase(const_iterator pos);                //删除迭代器指向的元素

erase(const_iterator start,const_iterator end); //删除迭代器从start到end之间的元素

clear();                                  //删除容器中所有的元素

数据存取

at(int idx);    //返回索引idx所指的数据

operator[];     //返回索引idx所指的数据

front();        //返回容器中第一个数据元素

back();         //返回容器中最后一个数据元素

互换容器

swap(vec); //将vec与本身的元素互换。

预留空间

reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物の哀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值