vector-入门(一)

这篇博客是作者学习C++ STL中vector的笔记,介绍了vector的基本概念和用法,包括作为动态数组的特点,如何声明及访问元素,强调了`vector::at()`和`vector::operator[]`的区别。博客提及vector的边界检查功能,并预告将在下一章继续讲解更多vector的成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       由于之前一直是用纯C语言的,所以对C++方面的东西,用得比较少。最近公司开始用C++,故特意去补习了下C++,对以前一直有疑问的东西,这次索性来个零基础重头学。

       本文为我个人的stl-vector学习笔记,如有任何不对的地方,请各位看官指正~

       本文参考了http://blog.youkuaiyun.com/phoebin/article/details/3864590 ,stl vector用法介绍。

 

百度百科-vector定义:

         vectorC++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够随意增加、减少数据。 

 

       为了可以使用vector,必须在你的头文件中包含下面的代码:

         #include <vector>

         vector属于std命名域的,因此需要通过命名限定,如下完成你的代码:

       using std::vector;

       vector<int> vInts; 
       或者连在一起,使用全名:

       std::vector<int>vInts;

        建议使用全局的命名域方式:

       using namespace std;

 

Vector有很多成员函数,这里举例依次讲解:

函数

表述

c.assign(beg,end)

c.assign(n,elem)

[beg; end)区间中的数据赋值给c

nelem的拷贝赋值给c

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;	

	//通过assign给其赋初值,拷贝两个int类型值,即33,到test的第一、第二单元格
	//该assign操作,是对vector容器的重新分配,在数据操作过程中,不建议用,会清空test中原有数据,造成非法访问
	test.assign(2, 33);

	printf("%d, %d\n", test[0], test[1]);

	system("pause");
}

 

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	int testArray[5] = {1, 2, 3, 4, 5};
	//创建一个int类型的vector容器
	vector<int>test;	

	//通过assign给其赋初值,拷贝从testArray[2]~testArray[4](不包括testArray[4]本身)中间的数据,到test,
	//即3、4会保存到test[0]、test[1]
	//该assign操作,是对vector容器的重新分配,在数据操作过程中,不建议用,会清空test中原有数据,造成非法访问
	test.assign(testArray + 2, testArray + 4);

	printf("%d, %d\n", test[0], test[1]);

	system("pause");
}

 

c.at(idx)

传回索引idx所指的数据,如果idx越界,抛出out_of_range


Vector中访问数据的方式有两种,vector::at()vector::operator[]

operator[]主要是为了与C语言进行兼容。它可以像C语言数组一样操作。但at()是我们的首选,因为at()进行了边界检查,如果访问超过了vector的范围,将抛出一个异常”,由于对抛异常trycatch没有概念,后续补强了,再另作修改。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;	

	test.assign(2, 23);

	printf("[] %d, %d, size:%d\n", test[0], test[1], test.size());

	printf("at() %d, %d, size:%d\n", test.at(0), test.at(1), test.size());

	system("pause");
}

 

c.pop_back()

删除最后一个数据。

c.push_back(elem)

在尾部加入一个数据。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;	

	test.assign(2, 23);

	test.push_back(33);

	printf("after push back :%d, %d, %d, size:%d\n", test.at(0), test.at(1), test.at(2), test.size());

	test.pop_back();
	
	//如果pop_back了,还对最后的值访问,会异常-非法访问
	//printf("after pop back :%d, %d, %d, size:%d\n", test.at(0), test.at(1), test.at(2), test.size());

	printf("after pop back :%d, %d, size:%d\n", test.at(0), test.at(1), test.size());

	system("pause");
}

 

c.back()

传回最后一个数据,不检查这个数据是否存在

c.front()

传回第一个数据。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;	

	test.push_back(11);
	test.push_back(22);
	test.push_back(33);

	printf("front:%d, back:%d\n", test.front(), test.back());

	system("pause");
}




c.begin()

传回迭代器重的可一个数据。

c.end()

指向迭代器中的最后一个数据地址。

c.insert(pos,elem)

c.insert(pos,n,elem)

c.insert(pos,beg,end)

pos位置插入一个elem拷贝,传回新数据位置。

pos位置插入nelem数据。无返回值。

pos位置插入在[beg,end)区间的数据。无返回值。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test1;
	vector<int>test2;

	//test2为 3个33
	test2.assign(3, 33);
	
	//test1中在0号位插入一个11
	test1.insert(test1.begin(), 11);
	printf("[0]%d\n", test1.at(0));

	//test1中在0号位插入2个22,  test1原本0号位的11会向后推移,变成22 22 11
	//test1.insert(test1.begin(), 2, 22);

	//test1中在1号位插入2个22,test1会变成11 22 22
	test1.insert(test1.begin()+1, 2, 22);
	printf("[0]%d, [1]%d, [2]%d\n", test1.at(0), test1.at(1), test1.at(2));

	//在test1的2号位插入test2从begin到end的所有数据
	test1.insert(test1.begin()+2, test2.begin(), test2.end());
	printf("[0]%d, [1]%d, [2]%d, [3]%d, [4]%d, [5]%d\n", 
		test1.at(0), test1.at(1), test1.at(2), test1.at(3), test1.at(4), test1.at(5));
	system("pause");
}

 

vector还有许多成员函数,鉴于文章太长,读起来不方便,我放到vector-入门(二)了



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值