由于之前一直是用纯C语言的,所以对C++方面的东西,用得比较少。最近公司开始用C++,故特意去补习了下C++,对以前一直有疑问的东西,这次索性来个零基础重头学。
本文为我个人的stl-vector学习笔记,如有任何不对的地方,请各位看官指正~
本文参考了http://blog.youkuaiyun.com/phoebin/article/details/3864590 ,stl vector用法介绍。
百度百科-vector定义:
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。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。 将n个elem的拷贝赋值给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的范围,将抛出一个异常”,由于对抛异常try,catch没有概念,后续补强了,再另作修改。
#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位置插入n个elem数据。无返回值。 在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-入门(二)了