vector
vector的定義
- vector()
- vector(size_type num, const value_type& val = value_type())
- 構造一個初始放入num個值為val的元素的Vector
- vector(const vector& from)
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(5, 0);
vector<int> v3(v2);
return 0;
}
vector的遍歷
- operator[ ]
- iterator begin()+ iterator end()
- begin返回一個指向當前vector起始元素的迭代器
- end返回一個指向當前vector末尾元素的下一位置的迭代器;如果你要訪問末尾元素,需要先將此迭代器自減1
- reverse_rbegin() + reverse_rend()
- rbegin返回指向當前vector末尾的逆迭代器(實際指向末尾的下一位置,而其內容為末尾元素的值)
- rend返回指向當前vector起始位置的逆迭代器
- 範圍for
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
for (size_t i = 0; i < v1.size(); ++i)
{
cout << v1[i] << " ";
}
cout << endl;
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
vector<int>::reverse_iterator rit = v1.rbegin();
while (rit != v1.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
return 0;
}
vector的容量
- size_type size() const
- size_type capacity() const
- 返回當前vector在重新進行記憶體分配以前所能容納的元素數量
- bool empty()
- 如果當前vector沒有容納任何元素,則empty()函式返回true,否則返回false
- void resize(size_type size, value_type val = value_type())
- 改變當前vector的大小為size,且對新建立的元素賦值val
- 如果size小於當前容器的大小,則將內容減少到其前size個元素,並刪除超出範圍的元素(並銷燬它們)
- 如果size大於當前容器的大小,則通過在末尾插入所需數量的元素來擴充套件內容,以達到size。如果指定了val,則將新元素初始化為val
- 如果size也大於當前容器容量,將自動重新分配已分配的儲存空間
- reserve
- 為當前vector預留至少共容納size個元素的空間
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
cout << v1.empty() << endl;
v1.resize(8, 0);
cout << v1.size() << endl;
v1.reserve(10);
cout << v1.capacity() << endl;
return 0;
}
vector的增刪查改
- void push_back(const value_type& val)
- void pop_back()
- iterator insert(iterator position, const value_type& val)
- 在指定位置position前插入值為val的元素,返回指向這個元素的迭代器
- iterator erase(iterator position)
- 刪除指定位置position的元素,返回刪除元素的下一位置的迭代器
- void swap(vector& x)
- find
- 查詢(這個是演算法模組實現,不是vector的成員介面),返回一個迭代器
- sort
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.pop_back();
v1.insert(v1.begin(), 0);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.erase(v1.begin());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
vector<int>::iterator pos = find(v1.begin(), v1.end(), 2);
if (pos != v1.end())
{
v1.erase(pos);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
vector<int> v2;
v2.push_back(10);
v2.push_back(40);
v2.push_back(30);
v2.push_back(20);
sort(v2.begin(), v2.end());
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
vector<int> v3;
v3.push_back(1);
v3.push_back(2);
v3.push_back(3);
v3.push_back(4);
v3.swap(v2);
for (auto e : v3)
{
cout << e << " ";
}
cout << endl;
return 0;
}
vector的模擬實現
#pragma once
#include<cstring>
#include<cassert>
#include<iostream>
using namespace std;
namespace bin {
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()
:_start(nullptr)
, _finish(nullptr)
,end_of_storage(nullptr)
{}
vector(const vector<T>& v)
:_start(nullptr)
,_finish(nullptr)
,end_of_storage(nullptr)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator=(vector<T> v)
{
this->swap(v);
return *this;
}
void swap(vector<T>& v)
{
::swap(_start, v._start);
::swap(_finish, v._finish);
::swap(end_of_storage, v.end_of_storage);
}
~vector()
{
delete[] _start;
_start = _finish = end_of_storage = nullptr;
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
iterator begin()
{
return _start;
}
const_iterator begin() const
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator end() const
{
return _finish;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
memcpy(tmp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
end_of_storage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(end() - 1);
}
void insert(iterator pos, const T& x)
{
assert(pos <= _finish);
if (_finish == end_of_storage)
{
size_t n = pos - _start;
size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
reserve(newcapacity);
pos = _start + n;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
}
iterator erase(iterator pos)
{
assert(pos < _finish);
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
++it;
}
--_finish;
return pos;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return end_of_storage - _start;
}
private:
iterator _start;
iterator _finish;
iterator end_of_storage;
};
}