vector容器及其实现
·
初始化
void test_vector1()
{
vector<int> v1;
//
vector<int> v2(10, 1);
vector<int> v3(v2.begin(), v2.end());
//
for (size_t i = 0; i < v1.size(); i++)
{
cout << v1[i] << ',';
}
cout << endl;
for (size_t i = 0; i < v2.size(); i++)
{
cout << v2[i] << ',';
}
cout << endl;
for (size_t i = 0; i < v3.size(); i++)
{
cout << v3[i] << ',';
}
cout << endl;
for (auto e : v1)
{
cout << e << ',';
}
cout << endl;
for (auto e : v2)
{
cout << e << ',';
}
cout << endl;
for (auto e : v3)
{
cout << e << ',';
}
cout << endl;
vector<int>::iterator it = v2.begin();
while (it != v2.end())
{
cout << *it << ',';
++it;
}
cout << endl;
}
reserve
void test()
{
vector<int> v;
v.reserve(50);
size_t sz = v.capacity();
cout << "capacity:" << sz << endl;
for (size_t i = 0; i < 100; i++)
{
v.push_back(i);
if (sz != v.capacity())
{
sz = v.capacity();
cout << "capacity grow" << sz << endl;
}
}
}
void test_vector2()
{
vector<int> v1(10, 1);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
//reserve resize
v1.reserve(20);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.reserve(15);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.reserve(5);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
test();
}
resize
void test_vector3()
{
vector<int> v1(10, 1);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
//reserve resize
v1.resize(20);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.resize(15);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
v1.resize(5);
cout << v1.size() << endl;
cout << v1.capacity() << endl;
}
插入接口和删除接口
void test_vector4()
{
vector<int> v1;
//vector<int> v2(10, 1);
//vector<int> v3(v2.begin(), v2.end());
//for (size_t i = 0; i < 10; i++)
//{
// v1.push_back(i);
// v1.pop_back();
//}
//for (auto e : v1)
//{
// cout << e << ',';
//}
//cout << endl;
//v2.insert(v2.begin(), 0);
//v2.erase(v2.begin());
//v2.insert(v2.begin()+3, 8);
//v2.erase(v2.begin()+3);
//v2.insert(v2.end(), 2);
//for (auto e : v2)
//{
// cout << e << ',';
//}
//cout << endl;
vector<int> v4(5, 0);
for (size_t i = 0; i < 5; i++)
{
cin >> v4[i];
}
for (auto e : v4)
{
cout << e << ',';
}
cout << endl;
vector<char> v2;
string s2;
//\0
}
可以存放内置类型和自定义类型
void test_vector5()
{
vector<string> v1;
string s1("xxxxxxxxxxxxx");
v1.push_back(s1);
v1.push_back("YYYYYYYYYYYYYYY");
for (auto e : v1)
{
cout << e << ',';
}
cout << endl;
vector<int> v(5, 1);
vector<vector<int>> vv(10, v);
vv[3][4] = 2;
}
具体实现
namespace jiege
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
//赋值和拷贝构造
//默认构造
//vector()
//{}
//c++11 强制生成默认构造
vector() = default;
//v2(v1)
//拷贝构造
vector(const vector<T>& v)
{
reserve(v.size());
for (auto& e : v)
{
push_back(e);
}
}
//类模板的成员函数还可以是模板函数
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
//用n个 val初值进行初始化
vector(size_t n, const T& val = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
~vector()
{
if (_start != nullptr)
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
}
void clear()
{
_start = _finish;
}
////v3=v1
//vector<T>& operator=(const vector<T>& v)
//{
// if (this != &v)
// {
// clear();
// reserve(v.size());
// for (auto& e : v)
// {
// push_back(e);
// }
// }
// return *this;
//}
//v3=v1
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_end_of_storage, v._end_of_storage);
}
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
//void reserve(size_t n)
//{
// if (n > capacity())
// {
// size_t old_size = _finish - _start;
// T* tmp = new T[n];
// memcpy(tmp, _start, size() * sizeof(T));
// delete[] _start;
// _start = tmp;
// _finish = tmp + old_size;
// _end_of_storage = tmp + n;
// }
//}
void reserve(size_t n)
{
if (n > capacity())
{
size_t old_size = _finish - _start;
T* tmp = new T[n];
//memcpy(tmp, _start, size() * sizeof(T));
for (size_t i = 0; i < old_size; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
_start = tmp;
_finish = tmp + old_size;
_end_of_storage = tmp + n;
}
}
void resize(size_t n,T val=T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish < _start + n)
{
*_finish = val;
_finish++;
}
}
}
bool empty()const
{
return _start == _finish;
}
size_t size()const
{
return _finish - _start;
}
size_t capacity()const
{
return _end_of_storage - _start;
}
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(!empty());
--_finish;
}
iterator insert(iterator pos,const T& x)
{
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
_finish++;
return pos;
}
void erase(iterator pos)
{
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
it++;
}
_finish--;
}
T& operator[](size_t i)
{
return _start[i];
}
const T& operator[](size_t i)const
{
return _start[i];
}
private:
iterator _start=nullptr;
iterator _finish = nullptr;
iterator _end_of_storage = nullptr;
};
template<class T>
void print_vector(const vector<T>& v)
{
//::无法分辨后面是类型还是静态变量
typename vector<T>::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << ' ';
}
cout << endl;
}
template<class Container>
void print_container(const Container& v)
{
//::无法分辨后面是类型还是静态变量
//typename Container::const_iterator it = v.begin();
auto it = v.begin();
while (it != v.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
//for (auto e : v)
//{
// cout << e << ' ';
//}
//cout << endl;
}
void test_vector1()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.pop_back();
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << ' ';
}
cout << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << ' ';
}
cout << endl;
print_vector(v);
}
void test_vector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//v.insert(v.begin() + 2, 100);
//print_vector(v);
int x;
cin >> x;
vector<int>::iterator p = find(v.begin(), v.end(), x);
v.insert(p, 200);
//insert 以后不论是否扩容 p都失效了,不能直接访问,需要更新p的位置
*p *= 10;//p还是指向原来的空间
print_vector(v);
}
void test_vector3()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//v.push_back(4);
//v.push_back(5);
//v.erase(v.begin());
std::vector<int>::iterator it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it=v.erase(it);//库里面返回erase的删除位置
}
else
{
++it;
}
}
//print_vector(v);
}
void test_vector4()
{
int i = int(2);
int j = int();
int k(2);
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(4);
v.push_back(4);
v.push_back(4);
v.push_back(4);
v.push_back(4);
print_vector(v);
cout << v.size() << endl;
cout << v.capacity() << endl;
v.resize(5);
print_vector(v);
cout << v.size() << endl;
cout << v.capacity() << endl;
v.resize(12);
print_vector(v);
cout << v.size() << endl;
cout << v.capacity() << endl;
v.resize(20);
print_vector(v);
cout << v.size() << endl;
cout << v.capacity() << endl;
}
void test_vector5()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int> v1 = v;
//print_vector(v1);
vector<int>v3;
v3 = v1;
print_vector(v3);
print_vector(v1);
}
void test_vector6()
{
//vector<int> v;
//v.push_back(1);
//v.push_back(2);
//v.push_back(3);
//v.push_back(4);
//v.push_back(4);
//v.push_back(4);
//print_vector(v);
//vector<int> v1(v.begin(), v.begin() + 3);
//print_vector(v1);
vector<string> v4(10, "1111111111111");
print_vector(v4);
vector<string> v5(10);
print_vector(v5);
//vector<string> v6(10,2);
//print_vector(v6);
}
void test_vector7()
{
vector<string> v;
v.push_back("111111111111111111111");
v.push_back("111111111111111111111");
v.push_back("111111111111111111111");
v.push_back("111111111111111111111");
print_container(v);
v.push_back("111111111111111111111");
print_container(v);
}
}
更多推荐




所有评论(0)