前言
对于vector,其实和string类是有点像的,但是里面的结构又有点不一样,所以有些操作是需要注意的
一 vector基本概念
1.我们使用vector的时候,可以把他当作一个数组来使用,只不过这个数组是可以自动扩容的
2.vector里面的数据是存在堆上面的,数组里面的数据是存在栈里面的,这个要区分
3.使用vector的时候需要包含#include<vector>头文件
二 vector的构造函数
函数原型:
default (1) | explicit vector (const allocator_type& alloc = allocator_type()); |
---|---|
fill (2) | explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); |
range (3) | template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); |
copy (4) | vector (const vector& x); |
1.vector (const allocator_type& alloc = allocator_type());//无参构造函数
2.vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());//将n个val拷贝给本身
3.vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());//使用迭代器把区间内的元素给本身
4.vector (const vector& x);//拷贝构造函数
1.explicit是限制隐式类型转换的。
2.对于第三个迭代器来说,设置为模板,这样就可以使用其他类型,而不单单限制于一种类型
3.对于const allocator_type& alloc = allocator_type()这个来说,他只是一个内存池,如果我们不写就用这个默认的,除非你觉得你比他的写得好,你就可以传进去??
为了方便测试,我们遍历容器的时候使用范围for来遍历,原理其实就是迭代器
#define _CRT_SECURE_NO_WARNINGS 1// constructing vectors#include <iostream>#include <vector>using namespace std;int main(){ // constructors use order as described above: vector<int> first; //构造一个空的vector vector<int> second(4, 100); //构造一个4个大小,val是100的vector vector<int> third(second.begin(), second.end()); //用迭代器去初始化third这个容器 vector<int> fourth(third); //用使用拷贝构造初始化fourth cout << "second的内容是:"; for (auto e : second) { cout << e <<' '; } cout << endl; cout << "third的内容是:"; for (auto e : third) { cout << e << ' '; } cout << endl; cout << "fourth的内容是:"; for (auto e : fourth) { cout << e << ' '; } cout << endl; // 迭代器构造也可以使用数组去构造: int myints[] = { 16,2,77,29 }; vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int)); cout << "这个fifth的内容是"; for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ' ' << *it; cout << endl; return 0;}
特别是最后一个要注意,是可以用数组去初始化的,但是用的比较少
三.vector的赋值操作
1.vector& operator= (const vector& x);//重载赋值运算符
2.assign(v.begin(),v.end());
//将[v.begin(),v.end())区间中的元素赋值给本身
3.assign(n,elem);
//将n个elem赋值给本身
对于上面的三种,我们一般使用的是第一种,其余的用迭代器是一样的
下面我们就先来测试一下
// constructing vectors// vector assignment#include <iostream>#include <vector>using namespace std;int main(){vector<int> foo(3, 0);vector<int> bar(5, 0);bar = foo;foo = std::vector<int>();cout << "Size of foo: " << int(foo.size()) << endl; cout << "Size of bar: " << int(bar.size()) << endl;return 0;}
四 vector的容量与大小
函数原型
1.size_type size() const;2.size_type size() const;3.void resize (size_type n, value_type val = value_type());4.size_type capacity() const;5.bool empty() const;6.void reserve (size_type n);7.void shrink_to_fit();
size Return size (public member function )//有效数据大小
max_size Return maximum size (public member function )//系统能够给出的最大有效数据容量
resize Change size (public member function )//重新指定容器的长度为num,若容器变长,则以val值填充新位置,如果容器变短,则末尾超过容器长度的元素被删除
capacity Return size of allocated storage capacity (public member function )//容器的容量
empty Test whether vector is empty (public member function )//如果容器为空,返回true,否则返回false
reserve Request a change in capacity (public member function )//预分配空间,改变容量,不改变size
shrink_to_fit Shrink to fit (public member function ) //请求容器减少其容量以匹配其当前大小
为了方便测试,先来测试shrink_to_fit
#define _CRT_SECURE_NO_WARNINGS 1// vector::shrink_to_fit#include <iostream>#include <vector>using namespace std;int main(){vector<int> myvector(100);cout << "1. capacity of myvector: " << myvector.capacity() << endl;myvector.resize(10);cout << "2. capacity of myvector: " << myvector.capacity() << endl;myvector.shrink_to_fit();cout << "3. capacity of myvector: " << myvector.capacity() << endl;return 0;}
可以看出在最后调用以后,把没用的空间给释放了
有了上面的测试,那下面就来其他函数的测试
#include <iostream>#include <vector>using namespace std;int main(){vector<int> myints;for (int i = 0; i < 10; i++) myints.push_back(i);cout << " size: " << myints.size() << endl;cout << "capacity:" << myints.capacity() << endl;cout << "max_size:" << myints.max_size() << endl;myints.resize(15);cout << "resize之后的size:" << myints.size() << endl;cout << "resize之后的capacity:" << myints.capacity() << endl;myints.reserve(100);cout << "reserve之后的size:" << myints.size() << endl;cout << "reserve之后的capacity" << myints.capacity() << endl;return 0;}
从运行结果就可以看出他们的用法和区别了
五 vector的元素修改
1.void push_back (const value_type& val);2.void pop_back();3.void insert (iterator position, size_type n, const value_type& val); iterator insert (iterator position, const value_type& val); void insert (iterator position, InputIterator first, InputIterator last);4.iterator erase (iterator position);iterator erase (iterator first, iterator last);5.void swap (vector& x);6.void clear();
对于insert和erase来说,他们可以采用迭代器进行插入删除 ,所以有多种表现形式
Modifiers:
push_back Add element at the end (public member function )//尾插一个元素
pop_back Delete last element (public member function )//尾删一个元素
insert Insert elements (public member function )//插入一个元素
erase Erase elements (public member function )//删除一个元素
swap Swap content (public member function )//交换元素
clear Clear content (public member function )//清理有效数据,不改变容量
测试案例
#include <iostream>#include <vector>using namespace std;int main(){ vector<int> myvector; for (int i = 0; i < 10; i++) { myvector.push_back(i);//尾插一个元素 } cout << "尾插后的数据大小:"; cout << myvector.size() << endl; myvector.pop_back(); cout << "尾删后的数据大小:"; cout << myvector.size() << endl; cout << "插入一个元素:"; myvector.insert(myvector.begin(), 1);//这里的位置需要用迭代器 cout << myvector.size() << endl; cout << "删除一个元素:"; myvector.erase(myvector.begin());//删除也需要用迭代器 cout << myvector.size() << endl; cout << "清除元素:"; myvector.clear(); cout << myvector.size() << endl; return 0;}
还有一个交换
// swap vectors#include <iostream>#include <vector>using namespace std;int main(){ vector<int> foo(3, 100); // 三个值为100 vector<int> bar(5, 200); // 五个值为200 foo.swap(bar); cout << "foo contains:"; for (unsigned i = 0; i < foo.size(); i++) std::cout << ' ' << foo[i]; cout << endl; cout << "bar contains:"; for (unsigned i = 0; i < bar.size(); i++) std::cout << ' ' << bar[i]; cout <<endl; return 0;}
六 vector数据的存取
1.reference operator[] (size_type n); 2.reference at (size_type n); 3.reference front();4.reference back();
以上四个都是由const形式的,就是用于给const类型的函数或者对象使用
operator[] Access element (public member function )//返回[]索引所指向的数据
at Access element (public member function )//返回n索引所指向的数据
front Access first element (public member function )//返回第一个元素
back Access first element (public member function )//返回最后一个元素
测试案例:
1.对于operator[]
// vector::operator[]#include <iostream>#include <vector>using namespace std;int main(){ vector<int> myvector(10); // 10 zero-initialized elements vector<int>::size_type sz = myvector.size(); // assign some values: for (unsigned i = 0; i < sz; i++) myvector[i] = i; // 使用[]对里面的元素实现逆置 for (unsigned i = 0; i < sz / 2; i++) { int temp; temp = myvector[sz - 1 - i]; myvector[sz - 1 - i] = myvector[i]; myvector[i] = temp; } cout << "myvector contains:"; for (unsigned i = 0; i < sz; i++) cout << ' ' << myvector[i]; cout << '\n'; return 0;}
2.对于at
// vector::operator[]#include <iostream>#include <vector>using namespace std;int main(){ vector<int> myvector(10); // 10 zero-initialized elements vector<int>::size_type sz = myvector.size(); // assign some values: for (unsigned i = 0; i < sz; i++) myvector.at(i) = i; cout << "myvector contains:"; for (unsigned i = 0; i < sz; i++) cout << ' ' << myvector.at(i); cout << '\n'; return 0;}
3.对于 front于back
#include <iostream>#include <vector>using namespace std;int main(){vector<int> myvector;myvector.push_back(78);myvector.push_back(16);//取第一元素减去最后的元素myvector.front() -= myvector.back();cout << "myvector.front() is now " << myvector.front() << endl;return 0;}
总结
在C++中vector用的很多,我之前都是看别人用,自己不会用,但是看多了也会了,现在把原理都搞清楚了一遍,用的时候也就可以更加得心应手了,喜欢的话,点赞加收藏!