172 lines
2.9 KiB
C++
172 lines
2.9 KiB
C++
|
#include "List.hpp"
|
||
|
|
||
|
using namespace Lenyiin;
|
||
|
|
||
|
void print(const List<int> <)
|
||
|
{
|
||
|
List<int>::const_iterator it = lt.begin();
|
||
|
while (it != lt.end())
|
||
|
{
|
||
|
cout << *it << " ";
|
||
|
++it;
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
void print_reverse(const List<int> <)
|
||
|
{
|
||
|
List<int>::const_reverse_iterator it = lt.rbegin();
|
||
|
while (it != lt.rend())
|
||
|
{
|
||
|
cout << *it << " ";
|
||
|
++it;
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
// 测试遍历
|
||
|
void test_1()
|
||
|
{
|
||
|
List<int> lt;
|
||
|
lt.push_back(1);
|
||
|
lt.push_back(2);
|
||
|
lt.push_back(3);
|
||
|
lt.push_back(4);
|
||
|
lt.push_back(5);
|
||
|
|
||
|
// iterator
|
||
|
cout << "iterator \t\t遍历: ";
|
||
|
List<int>::iterator it = lt.begin();
|
||
|
while (it != lt.end())
|
||
|
{
|
||
|
cout << *it << " ";
|
||
|
it++;
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// const_iterator
|
||
|
cout << "const_iterator \t\t遍历: ";
|
||
|
print(lt);
|
||
|
|
||
|
// for
|
||
|
cout << "for \t\t\t遍历: ";
|
||
|
for (auto &it : lt)
|
||
|
{
|
||
|
cout << it << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
cout << "for const \t\t遍历: ";
|
||
|
for (const auto &it : lt)
|
||
|
{
|
||
|
cout << it << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// reverse iterator
|
||
|
cout << "reverse iterator \t遍历: ";
|
||
|
List<int>::reverse_iterator rit = lt.rbegin();
|
||
|
while (rit != lt.rend())
|
||
|
{
|
||
|
cout << *rit << " ";
|
||
|
rit++;
|
||
|
}
|
||
|
cout << endl;
|
||
|
|
||
|
// const reverse iterator
|
||
|
cout << "const reverse iterator \t遍历: ";
|
||
|
print_reverse(lt);
|
||
|
}
|
||
|
|
||
|
// 测试插入删除
|
||
|
void test_2()
|
||
|
{
|
||
|
List<int> lt;
|
||
|
lt.push_back(1);
|
||
|
lt.push_back(2);
|
||
|
lt.push_back(3);
|
||
|
lt.push_back(4);
|
||
|
lt.push_back(5);
|
||
|
|
||
|
print(lt);
|
||
|
|
||
|
lt.erase(lt.begin());
|
||
|
print(lt);
|
||
|
|
||
|
lt.pop_back();
|
||
|
print(lt);
|
||
|
|
||
|
lt.pop_front();
|
||
|
print(lt);
|
||
|
|
||
|
lt.push_front(100);
|
||
|
print(lt);
|
||
|
}
|
||
|
|
||
|
void test_3()
|
||
|
{
|
||
|
// 默认构造
|
||
|
List<int> lt1;
|
||
|
lt1.push_back(1);
|
||
|
lt1.push_back(2);
|
||
|
lt1.push_back(3);
|
||
|
lt1.push_back(4);
|
||
|
lt1.push_back(5);
|
||
|
print(lt1);
|
||
|
|
||
|
// 拷贝构造
|
||
|
List<int> lt2(lt1);
|
||
|
print(lt2);
|
||
|
lt2.push_back(6);
|
||
|
lt2.push_back(7);
|
||
|
lt2.push_back(8);
|
||
|
lt2.push_back(9);
|
||
|
lt2.push_back(10);
|
||
|
print(lt2);
|
||
|
|
||
|
// 赋值运算
|
||
|
lt1 = lt2;
|
||
|
print(lt1);
|
||
|
}
|
||
|
|
||
|
// 模板
|
||
|
struct Date
|
||
|
{
|
||
|
int _year;
|
||
|
int _month;
|
||
|
int _day;
|
||
|
|
||
|
Date(int year = 0, int month = 1, int day = 1)
|
||
|
: _year(year), _month(month), _day(day)
|
||
|
{
|
||
|
}
|
||
|
};
|
||
|
|
||
|
void test_4()
|
||
|
{
|
||
|
List<Date> lt;
|
||
|
lt.push_back(Date());
|
||
|
lt.push_back(Date(2022, 2, 22));
|
||
|
lt.push_back(Date(2024, 9, 8));
|
||
|
|
||
|
List<Date>::iterator it = lt.begin();
|
||
|
while (it != lt.end())
|
||
|
{
|
||
|
// cout << *it << " ";
|
||
|
// operator-> operator*
|
||
|
cout << it->_year << "-" << it->_month << "-" << it->_day << endl; // 更喜欢这么去用
|
||
|
cout << (*it)._year << "-" << (*it)._month << "-" << (*it)._day << endl;
|
||
|
it++;
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// test_1();
|
||
|
// test_2();
|
||
|
// test_3();
|
||
|
test_4();
|
||
|
|
||
|
return 0;
|
||
|
}
|