#include
#include
using namespace std;
template
void printLst(const list& lst)
{
for (const T& e : lst)
cout << e << " ";
cout << endl;
}
void test()
{
list lst;
list lst2(5, 'a');
char str[] = "12345";
list lst3(str, str + 5);
list lst4(lst2.begin(), lst2.end());
list copy(lst3);
list::iterator it = lst3.begin();
while (it != lst3.end())
{
cout << *it << endl;
++it;
}
cout << endl;
list lst5(3, 1);
list::iterator it2 = lst5.begin();
while (it2 != lst5.end())
{
cout << *it2 << " ";
*it2 = 5;
++it2;
}
cout << endl;
it2 = lst5.begin();
while (it2 != lst5.end())
{
cout << *it2 << endl;
++it2;
}
cout << endl;
for (char ch : lst3)
{
cout << ch << " ";
}
cout << endl;
for (char& ch : lst3)
{
cout << ch << " ";
ch = 'X';
}
cout << endl;
for (char& ch : lst3)
{
cout << ch << " ";
}
cout << endl;
}
class A
{
public:
A(int a, int b)
: _a(a)
{
cout << “A(int,int)” << endl;
}
private:
int _a;
};
void test2()
{
list lst;
lst.push_back(1);
lst.push_front(0);
lst.push_front(-1);
lst.push_front(-2);
for (const auto& e : lst)
{
cout << e << " ";
cout << endl;
}
lst.pop_back();
printLst(lst);
lst.pop_front();
printLst(lst);
//list lst;
//A a(5, 6);
//lst.emplace_back(10,9);
//lst.emplace_back(-1, 0);
//lst.emplace_back(a);
}
void test3()
{
list lst;
lst.insert(lst.begin(), 1);
list::iterator it = lst.begin();
cout << “*it” << *it << endl;
printLst(lst);
lst.insert(lst.begin(), 3, 0);
cout << “*it” << *it << endl;
printLst(lst);
list copy(lst);
lst.insert(lst.end(), copy.begin(), copy.end());
cout << “*it” << *it << endl;
printLst(lst);
//删除会导致迭代器失效,调用删除接口之后,需要重新更新迭代器:1.获取erase返回值
//2.调用迭代器接口来重新获取
cout << "erase" < endl;
it=lst.erase(it);
cout << "*it " << *it << endl;
printLst(lst);
lst.erase(lst.begin(), lst.end());
printLst(lst);
cout << "resize:" << endl;
lst.resize(5);
printLst(lst);
lst.resize(7, 1);
printLst(lst);
cout << "remove" << endl;
lst.remove(1);
}#include
#include
#include
using namespace std;
void test1()
{
list lst;
list lst2(4, ‘a’);
char str[] = “12345”;
list lst3(str, str + 5);
list lst4(lst2.begin(), lst2.end());
list copy(lst3);
list lst5(3, 1);
auto it = lst5.begin();
while (it != lst5.end())
{
cout << *it << " ";
*it = 5;
it++;
cout << endl;
}
it = lst5.begin();
while (it != lst5.end())
{
cout << *it << " ";
it++;
cout << endl;
}
}
class A
{
public:
A(int a, int b)
: _a(a)
{
cout << “A(int,int)” << endl;
}
private:
int _a;
};
void test2()
{
list lst;
lst.insert(lst.begin(), 1);
list::iterator it = lst.begin();
}
void P(list& l)
{
for (auto& e : l)
cout << e << " ";
cout << endl;
}
void test3()
{
int array[] = { 1,2,3 };
list L(array, array + sizeof(array) / sizeof(array[0]));
P(L);
list lst2;
lst2.insert(lst2.begin(), 5,4);
list::iterator it = lst2.begin();
while (it != lst2.end())
{
cout << *it << endl;
it++;
}
cout << endl;
lst2.push_back(5);
it = lst2.begin();
while (it != lst2.end())
{
cout << *it << endl;
it++;
}
cout << endl;
lst2.pop_back();
lst2.pop_back();
it = lst2.begin();
while (it != lst2.end())
{
cout << *it << endl;
it++;
}
cout << endl;
auto pos = ++lst2.begin();
cout << *pos << endl;
}
void test4()
{
int array1[] = { 1,2,3 };
list L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
auto pos = ++L.begin();
cout << *pos << endl;
L.insert(pos, 4);
P(L);
L.insert(pos, 5, 5);
P(L);
list v{ 7,8,9 };
L.insert(pos, v.begin(), v.end());
P(L);
L.erase(pos);
P(L);
L.erase(L.begin(), L.end());
P(L);
}
void test5()
{
int array[] = { 1,2,3,4,5,2,2 };
list lst2(array, array + 6);
lst2.remove(2);
P(lst2);
}
void test6()
{
list lst;
lst.push_back(1);
lst.push_back(2);
lst.push_back(3);
list lst2;
lst2.push_back(4);
lst2.push_back(5);
P(lst);
cout << endl;
P(lst2);
lst.splice(lst.begin(), lst2);
P(lst);
P(lst2);
lst2.splice(lst2.begin(), lst, ++lst.begin(), --lst.end());
P(lst);
P(lst2);
}
int main()
{
test6();
return 0;
}
int main()
{
test3();
//test2();
return 0;
}