219 lines
3.6 KiB
C++
219 lines
3.6 KiB
C++
|
#include "Vector.hpp"
|
||
|
#include <string>
|
||
|
|
||
|
using namespace Lenyiin;
|
||
|
|
||
|
void print_Vector(const Vector<int> &v)
|
||
|
{
|
||
|
Vector<int>::const_iterator it = v.begin();
|
||
|
while (it != v.end())
|
||
|
{
|
||
|
cout << *it << " ";
|
||
|
++it;
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
// Vector 容器遍历
|
||
|
void test1()
|
||
|
{
|
||
|
Vector<int> v;
|
||
|
|
||
|
// 插入
|
||
|
v.push_back(1);
|
||
|
v.push_back(2);
|
||
|
v.push_back(3);
|
||
|
v.push_back(4);
|
||
|
v.push_back(5);
|
||
|
|
||
|
// 查看容量
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
// 1. 下标运算符 [] 遍历
|
||
|
cout << "1. 下标运算符 [] 遍历 \t\t";
|
||
|
for (size_t i = 0; i < v.size(); i++)
|
||
|
{
|
||
|
cout << v[i] << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// 2. 迭代器遍历
|
||
|
cout << "2. 迭代器遍历 \t\t\t";
|
||
|
Vector<int>::iterator it = v.begin();
|
||
|
while (it != v.end())
|
||
|
{
|
||
|
cout << *it << " ";
|
||
|
++it;
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// 3. const_iterator 迭代器遍历
|
||
|
cout << "3. const_iterator 迭代器遍历 \t";
|
||
|
print_Vector(v);
|
||
|
|
||
|
// 4. 范围 for 遍历
|
||
|
cout << "4. 范围 for 遍历 \t\t";
|
||
|
for (auto &e : v)
|
||
|
{
|
||
|
cout << e << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// 4. 范围 for 遍历 const
|
||
|
cout << "5. 范围 for 遍历 const \t\t";
|
||
|
for (const auto &e : v)
|
||
|
{
|
||
|
cout << e << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
void test2()
|
||
|
{
|
||
|
Vector<int> v;
|
||
|
v.push_back(1);
|
||
|
v.push_back(2);
|
||
|
v.push_back(3);
|
||
|
v.push_back(4);
|
||
|
v.push_back(5);
|
||
|
v.push_back(6);
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
// 随即插入
|
||
|
v.insert(v.begin(), 0);
|
||
|
print_Vector(v);
|
||
|
|
||
|
// 删除所有偶数
|
||
|
Vector<int>::iterator it = v.begin();
|
||
|
while (it != v.end())
|
||
|
{
|
||
|
if (*it % 2 == 0)
|
||
|
{
|
||
|
it = v.erase(it);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
it++;
|
||
|
}
|
||
|
}
|
||
|
print_Vector(v);
|
||
|
}
|
||
|
|
||
|
void test3()
|
||
|
{
|
||
|
Vector<int> v;
|
||
|
v.push_back(1);
|
||
|
v.push_back(2);
|
||
|
v.push_back(3);
|
||
|
v.push_back(4);
|
||
|
v.push_back(5);
|
||
|
v.push_back(6);
|
||
|
v.push_back(7);
|
||
|
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
// resize
|
||
|
v.resize(4);
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
v.resize(8);
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
v.resize(15);
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
// 清除
|
||
|
v.clear();
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
|
||
|
// reserve
|
||
|
v.reserve(20);
|
||
|
print_Vector(v);
|
||
|
cout << v.size() << endl;
|
||
|
cout << v.capacity() << endl
|
||
|
<< endl;
|
||
|
;
|
||
|
}
|
||
|
|
||
|
void test4()
|
||
|
{
|
||
|
// 默认构造
|
||
|
Vector<int> v1;
|
||
|
v1.push_back(1);
|
||
|
v1.push_back(2);
|
||
|
v1.push_back(3);
|
||
|
v1.push_back(4);
|
||
|
v1.push_back(5);
|
||
|
v1.push_back(6);
|
||
|
v1.push_back(7);
|
||
|
print_Vector(v1);
|
||
|
|
||
|
// 拷贝构造
|
||
|
Vector<int> v2(v1);
|
||
|
print_Vector(v2);
|
||
|
|
||
|
Vector<int> v3;
|
||
|
v3.push_back(10);
|
||
|
v3.push_back(20);
|
||
|
v3.push_back(30);
|
||
|
v3.push_back(40);
|
||
|
|
||
|
// 赋值
|
||
|
v1 = v3;
|
||
|
print_Vector(v1);
|
||
|
print_Vector(v3);
|
||
|
}
|
||
|
|
||
|
void test5()
|
||
|
{
|
||
|
// 模板
|
||
|
Vector<string> v;
|
||
|
v.push_back("111");
|
||
|
v.push_back("222");
|
||
|
v.push_back("333");
|
||
|
v.push_back("444");
|
||
|
|
||
|
for (auto e : v)
|
||
|
{
|
||
|
cout << e << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// test1();
|
||
|
// test2();
|
||
|
// test3();
|
||
|
// test4();
|
||
|
test5();
|
||
|
|
||
|
return 0;
|
||
|
}
|