171 lines
2.9 KiB
C++
171 lines
2.9 KiB
C++
#include "List.hpp"
|
|
|
|
using namespace Lenyiin;
|
|
|
|
void print(const List<int>& lt)
|
|
{
|
|
List<int>::const_iterator it = lt.begin();
|
|
while (it != lt.end())
|
|
{
|
|
cout << *it << " ";
|
|
++it;
|
|
}
|
|
cout << endl;
|
|
}
|
|
|
|
void print_reverse(const List<int>& lt)
|
|
{
|
|
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;
|
|
} |