目录
[2.2 迭代器(iterator) 及 对象的遍历访问](#2.2 迭代器(iterator) 及 对象的遍历访问)
[2.2.2 operator[] && at()](#2.2.2 operator[] && at())
[2.2.4 back() && front()](#2.2.4 back() && front())
[2.2.5 data](#2.2.5 data)
[2.3.1 size()](#2.3.1 size())
[2.3.2capacity() && reserve && resize](#2.3.2capacity() && reserve && resize)
[编辑2.3.3 empty() && clear()](#编辑2.3.3 empty() && clear())
[2.3.4 shrink_to_fit()](#2.3.4 shrink_to_fit())
[2.4.1 push_back && assign && insert](#2.4.1 push_back && assign && insert)
[2.4.2 find(算法库)](#2.4.2 find(算法库))
[2.4.3 pop_back && erase](#2.4.3 pop_back && erase)
前一章学习了string,那么学习vector的过程就会轻松很多,他们的接口用法大致都是相同的,对于剩余的容器,都可以以此类推学习,轻松拿捏。学习过程中,先学会如何使用,再来剖析底层大致如何实现,要做到完全理解底层,目前水平还是不够的,最后来实现一个简易版的vector。
一、vector的介绍
跟string一样他也是一个类模板,跟string不一样的是由于没被typedef过,定义对象时需要显示实例化,例如:vector<int> v;方括号中传的是类型,表示v对象中的数据类型是int类型,vector<string> v1;表示v1对象中的数据类型是string类型
- vector本质上可以理解为一个数组,数组是可变的,即可变大小的数组序列容器。
- 它的空间是连续的
- 它的底层是使用动态分配数组来存储元素
- 分配的空间比实际需要的存储空间更大来适应可能的空间增长。对于已经分配的空间,后续可能还要添加元素会让空间增大,所以一次性分配更大的空间。
- vector相比其他动态序列容器,访问元素更具高效性,对于其他不在末尾修改元素的容器,vector是可以做修改的
二、vector的使用(常用接口)
跟string一样,在学习vector的过程中,一定要去查看他的文档vector - C++ Reference (cplusplus.com)https://legacy.cplusplus.com/reference/vector/vector/,文档包含他的各种接口的介绍及用法,当然,其中可能会遇到新东西,这是很正常的,先了解,在学习使用。在学习了string类的接口之后,这里就不再过多详细介绍他们的用意了,而是直接使用,直到遇到新的接口再详说。在使用vector接口需包含头文件#include <vector>
2.1string类的成员函数
2.1.1构造函数
对于以下函数中的参数,不认识的先没必要去知道,先知道函数的作用及如何使用
| 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());
|-----------| | 用n个值初始化对象 | |
| range (3) | template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
|--------------------------------------| | 用一段区间的值初始化对象,模板参数可以是任何类型,如迭代器类型,指针类型 | |
| copy (4) | vector (const vector& x);
|------| | 拷贝构造 | |
|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;//构造一个空对象
vector<int> v1(4, 2);//用4个2初始化对象v1
for (auto e : v1)
{
cout << e;
}
cout << endl;
vector<int> v2(v1.begin(), v1.end());//用v1对象的迭代器区间的值初始化V2
for (auto e : v2)
{
cout << e;
}
cout << endl;
char arr[] = { 1,2,3,4,5 };
vector<int> v3(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化对象v3
for (auto e : v3)
{
cout << e;
}
cout << endl;
vector<int> v4(v3);//拷贝构造
for (auto e : v4)
{
cout << e;
}
cout << endl;
return 0;
}
2.1.2析构函数
~vector(); 这个没啥好说的,对象销毁前编译器会自动调用
2.1.3"="运算符重载函数
vector& operator=(const vector& x);
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;//构造一个空对象
vector<int> v1(4, 2);//用4个2初始化对象v1
v = v1;//v1赋值给v
for (auto e : v)
{
cout << e;
}
cout << endl;
return 0;
}
2.2 迭代器(iterator) 及 对象的遍历访问
2.2.1iterator
begin + end
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1(4, 2);//用4个2初始化对象v1
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
cout << *it;
it++;
}
cout << endl;
return 0;
}
rbegin + rend 反向迭代
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr)/sizeof(arr[0]));//用指针区间的值初始化v1
vector<int>::reverse_iterator it = v1.rbegin();
while (it != v1.rend())
{
cout << *it;
it++;
}
cout << endl;
return 0;
}
2.2.2 operator[] && at()
重载方括号,像数组一样使用下标进行访问 at()跟重载方括号差不多,返回对应位置的元素
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
cout << v1[i];
}
cout << endl;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
cout << v1.at(i);
}
cout << endl;
return 0;
}
2.2.4 back() && front()
分别获取对象的最后一个元素和第一个元素
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
cout << v1.front() << endl;
cout << v1.back() << endl;
return 0;
}
2.2.5 data
value_type* data() noexcept ;返回指向第一个元素的指针,指向元素可修改
const value_type* data() const noexcept;返回指向第一个元素的指针,指向元素不可修改
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
int* p = v1.data();
*p = 10;
p++;
*p = 20;
p[2] = 100;
for (auto e : v1)
{
cout << e << " ";
}
cout << endl ;
return 0;
}
2.3vector类对象的容量操作
2.3.1 size()
注意:vector类中没有提供length的操作
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
return 0;
}
2.3.2capacity() && reserve && resize
capacity():获取当前对象的容量大小,reserve():扩容,但不会影响数据,resize():既影响容量,又影响数据
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
cout << v1.capacity() << endl;
v1.reserve(20);
cout << v1.capacity() << endl;
v1.resize(30, 1);//扩容,在原先基础上,多余的空间初始化为1,默认初始化为0
cout << v1.capacity() << endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
return 0;
}
2.3.3 empty() && clear()
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
while (!v1.empty())
{
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
v1.clear();//清空所有元素
}
return 0;
}
2.3.4 shrink_to_fit()
**void shrink_to_fit();**缩容到与_size一样大,不会影响数据
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = { 1,2,3,4,5 };
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0]));//用指针区间的值初始化v1
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.reserve(100);
cout << v1.capacity() << endl;
v1.resize(3);//没有缩容,但改变了size大小
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.shrink_to_fit();//缩容到与size一样大小
cout << v1.capacity() << endl;
return 0;
}
2.4vector类对象的修改操作
2.4.1 push_back && assign && insert
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> v;
v.push_back(1.1);
v.push_back(2.2);
v.push_back(3.3);
v.push_back(4.4);
v.push_back(5.5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.assign(4, 10.1);//4个10.1赋值给对象,原先的内容会被替代,size也会改变
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
vector<double> v1;
v1.assign(v.begin() + 1, v.end() - 1);//用迭代器区间之间的值赋值给v1
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.insert(v1.end(), 20.2);//末尾插入20.2
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.insert(v1.end(), 2, 30.3);//末尾插入2个30.3
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.insert(v1.begin(), v.begin(), v.begin() + 2);//头部插入迭代器区间的值
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
return 0;
}
2.4.2 find(算法库)
注意:在vector中并没有提供find接口,但算法库中有find,所以可以使用算法库中的find,也要包含一下算法库的头文件。
**template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);左闭右开,**在迭代器区间查找值,找到,返回指向该值的迭代器。没找到,返回指向迭代器区间的last位置的迭代器
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> v;
v.push_back(1.1);
v.push_back(2.2);
v.push_back(3.3);
v.push_back(4.4);
v.push_back(5.5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
vector<double>::iterator it = find(v.begin(), v.end(), 2.2);
cout << *it << endl;
return 0;
}
2.4.3 pop_back && erase
函数原型:
**void pop_back();**删除最后一个位置的元素,size减1
iterator erase (iterator position); 删除迭代器指向位置的元素
**iterator erase (iterator first, iterator last);**删除迭代器指向区间的值
cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> v;
v.push_back(1.1);
v.push_back(2.2);
v.push_back(3.3);
v.push_back(4.4);
v.push_back(5.5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.pop_back();
v.pop_back();
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.erase(v.begin());
v.erase(v.begin(), v.begin() + 1);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
return 0;
}
三、vector模拟实现
cpp
//vector.h
#include <iostream>
#include <assert.h>
using namespace std;
namespace bit
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
vector()
{}
//vector(const vector<T>& x)
//{
// _start = new T[x.capacity()];
// memcpy(_start, x._start, x.size() * sizeof(T));
// _finish = _start + x.size();
// _end_of_storage = _start + x.capacity();
//}
vector(const vector<T>& v)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
//vector(size_t n, const T& x = T())//为了兼容模板参数,内置类型也有默认构造函数
//{
// if (_start)
// {
// delete[] _start;
// _start = new T[n];
// for (int i = 0; i < n; i++)
// {
// _start[i] = x;
// }
// _finish += n;
// _end_of_storage = _finish;
// }
//}
vector(size_t n, const T& x = T())
{
resize(n, x);
}
/* template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
if (_start)
{
delete[] _start;
_start = new T[last - first];
memcpy(_start, first, last - first);
_finish += last - first;
_end_of_storage = _finish;
}
}*/
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
first++;
}
}
vector<T>& operator=(vector<T> x)
{
swap(x);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
size_t size() const
{
return _finish - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t len = size();
iterator tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * (_finish - _start));//浅拷贝
for (int i = 0; i < len; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + len;
_end_of_storage = _start + n;
}
}
void resize(size_t n, T x = T())
{
if (n > size())
{
reserve(n);
while (_finish < _end_of_storage)
{
*_finish = x;
_finish++;
}
}
else
{
_finish = _start + n;
}
}
size_t capacity() const
{
return _end_of_storage - _start;
}
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
size_t newstorage = capacity() == 0 ? 4 : capacity() * 2;
reserve(newstorage);
}
//_start[_finish - _start] = x;
*_finish = x;
_finish++;
}
void pop_back()
{
if (size())
{
--_finish;
}
}
iterator insert(iterator position, const T& x)
{
assert(position >= _start && position <= _finish);
if (_finish == _end_of_storage)
{
size_t newstorage = capacity() == 0 ? 4 : capacity() * 2;
reserve(newstorage);
}
iterator end = _finish - 1;
while (end >= position)
{
*(end + 1) = *end;
}
*position = x;
_finish++;
return position;
}
iterator erase(iterator position)
{
assert(position < size());
if (size())
{
size_t len = _finish - position - 1;
for (int i = 0; i < len; i++)
{
*(position + i) = *(position + i + 1);
}
--_finish;
}
return position;
}
void swap(vector<T>& x)
{
std::swap(_start, x._start);
std::swap(_finish, x._finish);
std::swap(_end_of_storage, x._end_of_storage);
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
const T& operator[](size_t pos) const
{
return _start[pos];
}
void vectortest()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for (auto e : v)
{
cout << e;
}
cout << endl;
cout << v.capacity() << endl;
v.reserve(100);
cout << v.capacity() << endl;
vector<string> s;
s.push_back("a");
vector<string> s1 = s;
for (auto& e : s1)
{
cout << e;
}
cout << endl;
s.resize(10,"a");
for (auto& e : s)
{
cout << e;
}
cout << endl;
cout << s.capacity() << endl;
}
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
}
cpp
//test.cpp
#include "vector.h"
int main()
{
bit::vector<int> s;
s.vectortest();
return 0;
}