// TestList.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
template<class T> void print(const T& tle)
{
cout<<tle<<endl;
}
template<class T> void printlist(list<T>& mylist)
{
mylist.reverse();
list<T>::iterator itor;
for (itor = mylist.begin(); itor != mylist.end(); itor++)
{
cout<<*itor<<endl;
}
}
template<class T> void OutoScreen(const T& Ele)
{
cout<<Ele<<", ";
}
int _tmain(int argc, _TCHAR* argv[])
{
list<string> mylist_string;
mylist_string.push_front("1:Jack");
mylist_string.push_front("2:Tom");
mylist_string.push_front("3:Mike");
cout<<"mylist_string"<<endl;
for_each(mylist_string.begin(), mylist_string.end(), print<string>);
list<double> mylist_double;
mylist_double.push_front(10.0);
mylist_double.push_front(20.0);
mylist_double.push_front(30.0);
mylist_double.push_front(40.0);
mylist_double.push_front(50.0);
cout<<"mylist_double"<<endl;
for_each(mylist_double.begin(), mylist_double.end(), print<double>);
//复制
cout<<"list初始化"<<endl;
list<double> mylist_double2(mylist_double);
printlist<double>(mylist_double2);
//元素重置
cout<<"assign"<<endl;
list<double> mylist_double3;
mylist_double3.assign(mylist_double2.begin(), mylist_double2.end());
printlist(mylist_double3);
//交换
list<int> mylist_sour, mylist_des;
for (int i=0; i<6; i++)
{
mylist_sour.push_back(i);
mylist_des.push_back(i * 10);
}
cout<<"交换前"<<endl;
cout<<"mylist_sour"<<endl;
printlist(mylist_sour);
cout<<"mylist_des"<<endl;
printlist(mylist_des);
mylist_sour.swap(mylist_des);
cout<<"交换后"<<endl;
cout<<"mylist_sour"<<endl;
printlist(mylist_sour);
cout<<"mylist_des"<<endl;
printlist(mylist_des);
//删除
cout<<"删除list"<<endl;
list<int> mylt;
for (int i=0; i<10; i++)
{
mylt.push_back(i);
}
while (!mylt.empty())
{
for_each(mylt.begin(), mylt.end(), OutoScreen<int>);
mylt.erase(mylt.begin());
//mylt.pop_front();
cout<<endl;
}
//运算符 == < != > >=
/*
list<int> L1, L2;
L1.push_back(1);
L1.push_back(2);
L2.assign(L1.begin(), L1.end());
cout<<"L1 list"<<endl;
for_each(L1.begin(), L1.end(), print<int>);
cout<<"L2 list"<<endl;
for_each(L2.begin(), L2.end(), print<int>);
if (L1 == L2)
{
cout<<"L1 equal to L2"<<endl;
}
L1.push_back(3);
L2.push_back(2);
if (L1 > L2)
{
cout<<"L1 greater than L2"<<endl;
}*/
//合并
list<int> L1, L2, L3;
L1.push_back(1);
L1.push_back(5);
L2.push_back(2);
L2.push_back(3);
L3.push_back(7);
L3.push_back(8);
cout<<"L1合并L2"<<endl;
L1.merge(L2);
for_each(L1.begin(), L1.end(), print<int>);
cout<<"L1合并L3"<<endl;
L1.merge(L3);
for_each(L1.begin(), L1.end(), print<int>);
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
template<class T> void print(const T& tle)
{
cout<<tle<<endl;
}
template<class T> void printlist(list<T>& mylist)
{
mylist.reverse();
list<T>::iterator itor;
for (itor = mylist.begin(); itor != mylist.end(); itor++)
{
cout<<*itor<<endl;
}
}
template<class T> void OutoScreen(const T& Ele)
{
cout<<Ele<<", ";
}
int _tmain(int argc, _TCHAR* argv[])
{
list<string> mylist_string;
mylist_string.push_front("1:Jack");
mylist_string.push_front("2:Tom");
mylist_string.push_front("3:Mike");
cout<<"mylist_string"<<endl;
for_each(mylist_string.begin(), mylist_string.end(), print<string>);
list<double> mylist_double;
mylist_double.push_front(10.0);
mylist_double.push_front(20.0);
mylist_double.push_front(30.0);
mylist_double.push_front(40.0);
mylist_double.push_front(50.0);
cout<<"mylist_double"<<endl;
for_each(mylist_double.begin(), mylist_double.end(), print<double>);
//复制
cout<<"list初始化"<<endl;
list<double> mylist_double2(mylist_double);
printlist<double>(mylist_double2);
//元素重置
cout<<"assign"<<endl;
list<double> mylist_double3;
mylist_double3.assign(mylist_double2.begin(), mylist_double2.end());
printlist(mylist_double3);
//交换
list<int> mylist_sour, mylist_des;
for (int i=0; i<6; i++)
{
mylist_sour.push_back(i);
mylist_des.push_back(i * 10);
}
cout<<"交换前"<<endl;
cout<<"mylist_sour"<<endl;
printlist(mylist_sour);
cout<<"mylist_des"<<endl;
printlist(mylist_des);
mylist_sour.swap(mylist_des);
cout<<"交换后"<<endl;
cout<<"mylist_sour"<<endl;
printlist(mylist_sour);
cout<<"mylist_des"<<endl;
printlist(mylist_des);
//删除
cout<<"删除list"<<endl;
list<int> mylt;
for (int i=0; i<10; i++)
{
mylt.push_back(i);
}
while (!mylt.empty())
{
for_each(mylt.begin(), mylt.end(), OutoScreen<int>);
mylt.erase(mylt.begin());
//mylt.pop_front();
cout<<endl;
}
//运算符 == < != > >=
/*
list<int> L1, L2;
L1.push_back(1);
L1.push_back(2);
L2.assign(L1.begin(), L1.end());
cout<<"L1 list"<<endl;
for_each(L1.begin(), L1.end(), print<int>);
cout<<"L2 list"<<endl;
for_each(L2.begin(), L2.end(), print<int>);
if (L1 == L2)
{
cout<<"L1 equal to L2"<<endl;
}
L1.push_back(3);
L2.push_back(2);
if (L1 > L2)
{
cout<<"L1 greater than L2"<<endl;
}*/
//合并
list<int> L1, L2, L3;
L1.push_back(1);
L1.push_back(5);
L2.push_back(2);
L2.push_back(3);
L3.push_back(7);
L3.push_back(8);
cout<<"L1合并L2"<<endl;
L1.merge(L2);
for_each(L1.begin(), L1.end(), print<int>);
cout<<"L1合并L3"<<endl;
L1.merge(L3);
for_each(L1.begin(), L1.end(), print<int>);
return 0;
}