文章目录 list容器 1.构造函数 2.赋值和交换 3.大小操作 4.插入和删除 5.数据存取 6.反转和排序
list容器
1.构造函数
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list> using namespace std;
void printList ( const list< int > & d)
{ for ( list< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ list< int > L; for ( int i = 0 ; i < 5 ; i++ ) { L. push_back ( i) ; } printList ( L) ; list< int > L1 ( L. begin ( ) , L. end ( ) ) ; printList ( L1) ; list< int > L3 ( 5 , 100 ) ; printList ( L3) ; list< int > L4 ( L3) ; printList ( L4) ; return ;
}
int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
2.赋值和交换
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list> using namespace std;
void printList ( const list< int > & d)
{ for ( list< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ list< int > L; for ( int i = 0 ; i < 5 ; i++ ) { L. push_back ( i) ; } printList ( L) ; list< int > L1; L1 = L; printList ( L1) ; list< int > L2; L2. assign ( L1. begin ( ) , L1. end ( ) ) ; printList ( L2) ; list< int > L3; L3. assign ( 5 , 50 ) ; printList ( L3) ; return ;
} void test02 ( )
{ list< int > L1; for ( int i = 0 ; i < 5 ; i++ ) { L1. push_back ( i * 10 ) ; } cout << "交换前的L1容器的元素:" ; printList ( L1) ; list< int > L2; L2. assign ( 5 , 60 ) ; cout << "交换前的L2容器的元素:" ; printList ( L2) ; L1. swap ( L2) ; cout << "交换后的L1容器的元素:" ; printList ( L1) ; cout << "交换后的L2容器的元素:" ; printList ( L2) ; return ;
} int main ( )
{ test01 ( ) ; cout << endl << "测试swap交换容器元素" << endl; test02 ( ) ; return 0 ;
}
运行结果
3.大小操作
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list> using namespace std;
void printList ( const list< int > & d)
{ for ( list< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ list< int > L; for ( int i = 0 ; i < 5 ; i++ ) { L. push_back ( i) ; } printList ( L) ; if ( 0 != L. empty ( ) ) { cout << "容器为空" << endl; return ; } cout << "容器的大小:" << L. size ( ) << endl; L. resize ( 10 ) ; printList ( L) ; L. resize ( 2 ) ; printList ( L) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
4.插入和删除
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list> using namespace std;
void printList ( const list< int > & d)
{ for ( list< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
} void test01 ( )
{ list< int > L; L. push_back ( 10 ) ; L. push_back ( 20 ) ; L. push_front ( 100 ) ; L. push_front ( 200 ) ; printList ( L) ; L. pop_back ( ) ; L. pop_front ( ) ; printList ( L) ; list< int > :: iterator it = L. begin ( ) ; it++ ; L. insert ( it, 2 , 3000 ) ; printList ( L) ; L. erase ( L. begin ( ) ) ; printList ( L) ; L. remove ( 3000 ) ; printList ( L) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
5.数据存取
工程代码
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list> using namespace std;
void test01 ( )
{ list< int > L; L. push_back ( 10 ) ; L. push_back ( 20 ) ; L. push_back ( 30 ) ; L. push_back ( 40 ) ; cout << "容器第一个元素: " << L. front ( ) << endl; cout << "容器最后一个元素: " << L. back ( ) << endl; list< int > :: iterator it = L. begin ( ) ; it++ ; it-- ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果
6.反转和排序
代码工程
# define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <list>
using namespace std;
void printList ( const list< int > & d)
{ for ( list< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ ) { cout << * it << " " ; } cout << endl;
}
class MyCmp
{
public: bool operator ( ) ( int v1, int v2) { return v1 > v2; }
} ;
void test01 ( )
{ list< int > L; L. push_back ( 10 ) ; L. push_back ( 30 ) ; L. push_back ( 20 ) ; L. push_back ( 40 ) ; cout << "反转前:" ; printList ( L) ; L. reverse ( ) ; cout << "反转后:" ; printList ( L) ; L. sort ( ) ; cout << "排序(默认升序):" ; printList ( L) ; L. sort ( MyCmp ( ) ) ; cout << "排序(降序):" ; printList ( L) ; return ;
} int main ( )
{ test01 ( ) ; return 0 ;
}
运行结果