初识STL
一、vector存放内置数据类型
# include <iostream>
using namespace std;
# include <vector>
# include <algorithm>
void myPrint ( int val)
{
cout << val << endl;
}
void test01 ( )
{
vector < int > v;
v. push_back ( 10 ) ;
v. push_back ( 20 ) ;
v. push_back ( 30 ) ;
v. push_back ( 40 ) ;
vector< int > :: iterator itBegin = v. begin ( ) ;
vector< int > :: iterator itEnd = v. end ( ) ;
for_each ( v. begin ( ) , v. end ( ) , myPrint) ;
}
int main ( )
{
test01 ( ) ;
}
二、vector容器中存放自定义数据类型
# include <iostream>
# include <vector>
# include <string>
using namespace std;
class Person
{
public :
Person ( string name, int age)
{
this -> m_Name = name;
this -> m_Age = age;
}
string m_Name;
int m_Age;
} ;
void test01 ( )
{
vector< Person> v;
Person p1 ( "aaa" , 10 ) ;
Person p2 ( "bbb" , 20 ) ;
Person p3 ( "bbb" , 30 ) ;
Person p4 ( "bbb" , 40 ) ;
v. push_back ( p1) ;
v. push_back ( p2) ;
v. push_back ( p3) ;
v. push_back ( p4) ;
for ( vector< Person> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << "姓名:" << ( * it) . m_Name << "年龄" << ( * it) . m_Age << endl;
cout << "姓名:" << it-> m_Name << "年龄" << ( * it) . m_Age << endl;
}
}
void test02 ( )
{
vector< Person* > v;
Person p1 ( "aaa" , 10 ) ;
Person p2 ( "bbb" , 20 ) ;
Person p3 ( "bbb" , 30 ) ;
Person p4 ( "bbb" , 40 ) ;
v. push_back ( & p1) ;
v. push_back ( & p2) ;
v. push_back ( & p3) ;
v. push_back ( & p4) ;
for ( vector< Person* > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << "姓名:" << ( * it) -> m_Name << "年龄:" << ( * it) -> m_Age << endl;
}
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
}
三、vextor容器嵌套容器
# include <iostream>
using namespace std;
# include <vector>
void test01 ( )
{
vector < vector< int >> v;
vector< int > v1;
vector< int > v2;
vector< int > v3;
vector< int > v4;
for ( int i = 0 ; i < 4 ; i++ )
{
v1. push_back ( i + 1 ) ;
v2. push_back ( i + 2 ) ;
v3. push_back ( i + 3 ) ;
v4. push_back ( i + 4 ) ;
}
v. push_back ( v1) ;
v. push_back ( v2) ;
v. push_back ( v3) ;
v. push_back ( v4) ;
for ( vector< vector< int >> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
for ( vector< int > :: iterator vit = ( * it) . begin ( ) ; vit != ( * it) . end ( ) ; vit++ )
{
cout << * vit << " " ;
}
cout << endl;
}
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
四、string容器构造函数
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string s1;
const char * str = "hello world" ;
string s2 ( str) ;
cout << "s2=" << s2 << endl;
string s3 ( s2) ;
cout << "s3=" << s3 << endl;
string s4 ( 10 , 'a' ) ;
cout << "s4=" << s4 << endl;
}
int main ( )
{
test01 ( ) ;
}
五、string 的赋值操作
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string str1;
str1 = "hello world" ;
cout << "str1=" << str1 << endl;
string str2;
str2 = str1;
cout << "str2=" << str2 << endl;
string str3;
str3 = 'a' ;
cout << "str3=" << str3 << endl;
string str4;
str4. assign ( "hello c++" ) ;
cout << "str4=" << str4 << endl;
string str5;
str5. assign ( "hello c++" , 5 ) ;
cout << "str5=" << str5 << endl;
string str6;
str6. assign ( str5) ;
cout << "str6=" << str6 << endl;
string str7;
str7. assign ( 10 , 'w' ) ;
cout << "str7=" << str7 << endl;
}
int main ( )
{
test01 ( ) ;
}
六、字符串拼接
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string str1 = "我" ;
str1 += "爱玩游戏" ;
cout << "str=" << str1 << endl;
str1 += ';' ;
cout << "str1=" << str1 << endl;
string str2 = "LOL DNF" ;
str1 += str2;
cout << "str1=" << str1 << endl;
string str3 = "I" ;
str3. append ( "love" ) ;
cout << "str3=" << str3 << endl;
str3. append ( "game abcde" , 4 ) ;
cout << "str3=" << str3 << endl;
str3. append ( str2, 4 , 3 ) ;
cout << "str3=" << str3 << endl;
}
int main ( )
{
test01 ( ) ;
}
七、字符串查找和替换
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string str1 = "abcdefgde" ;
int pos = str1. find ( "de" ) ;
if ( pos == - 1 )
{
cout << "未找到字符串" << endl;
}
else
{
cout << "pos=" << pos << endl;
}
pos = str1. rfind ( "de" ) ;
cout << "pos=" << pos << endl;
}
void test02 ( )
{
string str1 = "abcdefg" ;
str1. replace ( 1 , 3 , "1111" ) ;
cout << "str1=" << str1 << endl;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
}
八、 string字符串比较
# include <iostream>
using namespace std;
void test01 ( )
{
string str1 = "hello" ;
string str2 = "xello" ;
if ( str1. compare ( str2) == 0 )
{
cout << "str1=str2" << endl;
}
else if ( str1. compare ( str2) > 0 )
{
cout << "str1>str2" << endl;
}
else
{
cout << "str1<str2" << endl;
}
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
九、字符串存取
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string str = "Hello" ;
for ( int i = 0 ; i < str. size ( ) ; i++ )
{
cout << str[ i] << " " ;
}
cout << endl;
for ( int i = 0 ; i < str. size ( ) ; i++ )
{
cout << str. at ( i) << " " ;
}
str[ 0 ] = 'x' ;
cout << "str=" << str << endl;
str. at ( 1 ) = 'x' ;
cout << "str=" << str << endl;
}
int main ( )
{
test01 ( ) ;
}
十、sting插入和删除
# include <iostream>
# include <string>
using namespace std;
void test01 ( )
{
string str = "hello" ;
str. insert ( 1 , "111" ) ;
cout << "str=" << str << endl;
str. erase ( 1 , 3 ) ;
cout << "str=" << str << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
十一、 string子串
# include <iostream>
using namespace std;
# include <string>
void test01 ( )
{
string str = "abcdef" ;
string subStr = str. substr ( 1 , 3 ) ;
cout << "subStr=" << subStr << endl;
}
void test02 ( )
{
string email = "zhangsan@sina.com" ;
int pos = email. find ( "@" ) ;
string usrName = email. substr ( 0 , pos) ;
cout << usrName << endl;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
return 0 ;
}
十二、 vector容器
# include <iostream>
# include <vector>
using namespace std;
void printVector ( vector< int > & v)
{
for ( vector< int > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
printVector ( v1) ;
vector< int > v2 ( v1. begin ( ) , v1. end ( ) ) ;
printVector ( v2) ;
vector< int > v3 ( 10 , 100 ) ;
printVector ( v3) ;
vector< int > v4 ( v3) ;
printVector ( v4) ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
十三、vector容器赋值操作
# include <iostream>
using namespace std;
# include <vector>
void printVector ( vector< int > & v)
{
for ( vector< int > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
printVector ( v1) ;
vector< int > v2;
v2 = v1;
printVector ( v2) ;
vector< int > v3;
v3. assign ( v1. begin ( ) , v1. end ( ) ) ;
printVector ( v3) ;
vector< int > v4;
v4. assign ( 10 , 100 ) ;
printVector ( v4) ;
}
int main ( )
{
test01 ( ) ;
}
十四、vector容量和大小
# include <iostream>
# include <vector>
using namespace std;
void printVector ( vector< int > & v)
{
for ( vector< int > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
printVector ( v1) ;
if ( v1. empty ( ) )
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量为:" << v1. capacity ( ) << endl;
cout << "v1的大小为:" << v1. size ( ) << endl;
}
v1. resize ( 15 , 100 ) ;
printVector ( v1) ;
v1. resize ( 5 ) ;
printVector ( v1) ;
}
int main ( )
{
test01 ( ) ;
system ( "pause" ) ;
return 0 ;
}
十五、 vector插入和删除
# include <iostream>
# include <vector>
using namespace std;
void printVector ( vector< int > & v)
{
for ( vector< int > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
v1. push_back ( 10 ) ;
v1. push_back ( 20 ) ;
v1. push_back ( 30 ) ;
v1. push_back ( 40 ) ;
printVector ( v1) ;
v1. pop_back ( ) ;
printVector ( v1) ;
v1. insert ( v1. begin ( ) , 1000 ) ;
printVector ( v1) ;
v1. insert ( v1. begin ( ) , 2 , 1000 ) ;
printVector ( v1) ;
v1. erase ( v1. begin ( ) ) ;
printVector ( v1) ;
v1. clear ( ) ;
printVector ( v1) ;
}
int main ( )
{
test01 ( ) ;
}
十六、vector数据存取
# include <iostream>
# include <vector>
using namespace std;
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
for ( int i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1[ i] << " " ;
}
cout << endl;
for ( int i = 0 ; i < v1. size ( ) ; i++ )
{
cout << v1. at ( i) << " " ;
}
cout << endl;
cout << "第一个元素为:" << v1. front ( ) << endl;
cout << "最后一个元素为:" << v1. back ( ) << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
十七、 vector互换容器
# include <iostream>
# include <vector>
using namespace std;
void printVector ( vector< int > & v)
{
for ( vector< int > :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
vector< int > v1;
for ( int i = 0 ; i < 10 ; i++ )
{
v1. push_back ( i) ;
}
cout << "交换前:" << endl;
printVector ( v1) ;
vector< int > v2;
for ( int i = 10 ; i > 0 ; i-- )
{
v2. push_back ( i) ;
}
printVector ( v2) ;
cout << "交换后" << endl;
v1. swap ( v2) ;
printVector ( v1) ;
printVector ( v2) ;
}
void test02 ( )
{
vector< int > v;
for ( int i = 0 ; i < 100000 ; i++ )
{
v. push_back ( i) ;
}
cout << "v的容量为:" << v. capacity ( ) << endl;
cout << "v的大小为:" << v. size ( ) << endl;
v. resize ( 3 ) ;
cout << "v的容量为" << v. capacity ( ) << endl;
cout << "v的大小为" << v. size ( ) << endl;
vector< int > ( v) . swap ( v) ;
cout << "v的容量为:" << v. capacity ( ) << endl;
cout << "v的大小为:" << v. size ( ) << endl;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
}
十八、vector容器reserve 预留空间
# include <iostream>
using namespace std;
# include <vector>
void test01 ( )
{
vector< int > v;
v. reserve ( 100000 ) ;
int num = 0 ;
int * p = NULL ;
for ( int i = 0 ; i < 100000 ; i++ )
{
v. push_back ( i) ;
if ( p != & v[ 0 ] )
{
p = & v[ 0 ] ;
num++ ;
}
}
cout << "num=" << num << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
十九、deque容器 构造函数
# include <iostream>
using namespace std;
# include <deque>
void printDeque ( const deque< int > & d)
{
for ( deque< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
deque< int > d1;
for ( int i = 0 ; i < 10 ; i++ )
{
d1. push_back ( i) ;
}
printDeque ( d1) ;
deque< int > d2 ( d1. begin ( ) , d1. end ( ) ) ;
printDeque ( d2) ;
deque< int > d3 ( 10 , 100 ) ;
printDeque ( d3) ;
deque< int > d4 ( d3) ;
printDeque ( d4) ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十、deque容器的复制操作
# include <iostream>
using namespace std;
# include <deque>
void printDeque ( const deque< int > & d)
{
for ( deque< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
deque< int > d1;
for ( int i = 0 ; i < 10 ; i++ )
{
d1. push_back ( i) ;
}
printDeque ( d1) ;
deque< int > d2;
printDeque ( d2) ;
deque< int > d3;
d3. assign ( d1. begin ( ) , d1. end ( ) ) ;
printDeque ( d3) ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十一、deque大小操作
# include <iostream>
using namespace std;
# include <deque>
void printDeque ( const deque< int > & d)
{
for ( deque< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
deque< int > d1;
for ( int i = 0 ; i < 10 ; i++ )
{
d1. push_back ( i) ;
}
printDeque ( d1) ;
if ( d1. empty ( ) )
{
cout << "为空" << endl;
}
else
{
cout << "d1不为空" << endl;
cout << "d1的大小为" << d1. size ( ) << endl;
}
d1. resize ( 15 , 1 ) ;
printDeque ( d1) ;
d1. resize ( 5 ) ;
printDeque ( d1) ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十二·、deque的删除插入操作
# include <iostream>
using namespace std;
# include <deque>
void printDeque ( const deque< int > & d)
{
for ( deque< int > :: const_iterator it = d. begin ( ) ; it != d. end ( ) ; it++ )
{
cout << * it << " " ;
}
cout << endl;
}
void test01 ( )
{
deque< int > d1;
d1. push_back ( 10 ) ;
d1. push_back ( 20 ) ;
d1. push_front ( 100 ) ;
d1. push_front ( 200 ) ;
printDeque ( d1) ;
d1. pop_back ( ) ;
printDeque ( d1) ;
d1. pop_front ( ) ;
printDeque ( d1) ;
}
void test02 ( )
{
deque< int > d1;
d1. push_back ( 10 ) ;
d1. push_back ( 20 ) ;
d1. push_back ( 100 ) ;
d1. push_back ( 200 ) ;
printDeque ( d1) ;
d1. insert ( d1. begin ( ) , 1000 ) ;
printDeque ( d1) ;
d1. insert ( d1. begin ( ) , 2 , 10000 ) ;
printDeque ( d1) ;
deque< int > d2;
d2. push_back ( 1 ) ;
d2. push_back ( 2 ) ;
d2. push_back ( 3 ) ;
d1. insert ( d1. begin ( ) , d2. begin ( ) , d2. end ( ) ) ;
printDeque ( d1) ;
}
void test03 ( )
{
deque< int > d1;
d1. push_back ( 10 ) ;
d1. push_back ( 20 ) ;
d1. push_front ( 100 ) ;
d1. push_front ( 200 ) ;
deque< int > :: iterator it = d1. begin ( ) ;
it++ ;
d1. erase ( it) ;
printDeque ( d1) ;
d1. erase ( d1. begin ( ) , d1. end ( ) ) ;
printDeque ( d1) ;
}
int main ( )
{
test01 ( ) ;
test02 ( ) ;
test03 ( ) ;
return 0 ;
}
二十三、deque容器数据存取
# include <iostream>
using namespace std;
# include <deque>
void test01 ( )
{
deque< int > d;
d. push_back ( 10 ) ;
d. push_back ( 20 ) ;
d. push_back ( 30 ) ;
d. push_front ( 200 ) ;
d. push_front ( 300 ) ;
for ( int i = 0 ; i < d. size ( ) ; i++ )
{
cout << d[ i] << " " ;
}
cout << endl;
for ( int i = 0 ; i < d. size ( ) ; i++ )
{
cout << d. at ( i) << " " ;
}
cout << endl;
for ( int i= 0 ; i< d. size ( ) ; i++ )
cout << endl;
cout << "第一个元素为:" << d. front ( ) << endl;
cout << "最后一个元素为" << d. back ( ) << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十四、deque排序
# include <iostream>
using namespace std;
# include <deque>
# include <algorithm>
void printDeque ( const deque< int > & d)
{
for ( deque< int > :: const_iterator it = d. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << * it << " " ;
}
}
void test01 ( )
{
deque< int > d;
printDeque ( d) ;
d. push_back ( 10 ) ;
d. push_back ( 20 ) ;
d. push_back ( 30 ) ;
d. push_front ( 100 ) ;
d. push_front ( 200 ) ;
d. push_front ( 300 ) ;
printDeque ( d) ;
sort ( d. begin ( ) , d. end ( ) ) ;
cout << "排序后;" << endl;
printDeque ( d) ;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十五、案例
# include <iostream>
# include <string>
using namespace std;
# include <vector>
# include <deque>
# include <algorithm>
# include <ctime>
class Person
{
public :
Person ( string name, int score)
{
this -> m_Name = name;
this -> m_Score = score;
}
string m_Name;
int m_Score;
} ;
void createPerson ( vector< Person> & v)
{
string nameSeed = "ABCDE" ;
for ( int i = 0 ; i < 5 ; i++ )
{
string name = "选手" ;
name += nameSeed[ i] ;
int score = 0 ;
Person p ( name, score) ;
v. push_back ( p) ;
}
}
void setScore ( vector< Person> & v)
{
for ( vector< Person> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
deque< int > d;
for ( int i = 0 ; i < 10 ; i++ )
{
int score = rand ( ) % 41 + 60 ;
d. push_back ( score) ;
}
sort ( d. begin ( ) , d. end ( ) ) ;
d. pop_back ( ) ;
d. pop_front ( ) ;
int sum = 0 ;
for ( deque< int > :: iterator dit = d. begin ( ) ; dit != d. end ( ) ; dit++ )
{
sum += * dit;
}
int avg = sum / d. size ( ) ;
it-> m_Score = avg;
}
}
void showScore ( vector< Person> & v)
{
for ( vector< Person> :: iterator it = v. begin ( ) ; it != v. end ( ) ; it++ )
{
cout << "姓名:" << it-> m_Name << "平均分:" << it-> m_Score << endl;
}
}
int main ( )
{
srand ( ( unsigned int ) time ( NULL ) ) ;
vector< Person> v;
createPerson ( v) ;
setScore ( v) ;
showScore ( v) ;
system ( "pause" ) ;
return 0 ;
}
二十六、栈stack容器
# include <iostream>
using namespace std;
# include <stack>
void test01 ( )
{
stack< int > s;
s. push ( 10 ) ;
s. push ( 20 ) ;
s. push ( 30 ) ;
s. push ( 40 ) ;
while ( ! s. empty ( ) )
{
cout << "栈项元素:" << s. top ( ) << endl;
s. pop ( ) ;
}
cout << "栈的大小:" << s. size ( ) << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十七、queue容器常用接口
# include <iostream>
# include <queue>
# include <string>
using namespace std;
class Person
{
public :
Person ( string name, int age)
{
this -> m_Name = name;
this -> m_Age = age;
}
string m_Name;
int m_Age;
} ;
void test01 ( )
{
queue< Person> q;
Person p1 ( "唐僧" , 30 ) ;
Person p2 ( "孙悟空" , 1000 ) ;
Person p3 ( "猪八戒" , 900 ) ;
Person p4 ( "沙僧" , 800 ) ;
q. push ( p1) ;
q. push ( p2) ;
q. push ( p3) ;
q. push ( p4) ;
while ( ! q. empty ( ) )
{
cout << "队头元素—姓名:" << q. front ( ) . m_Name << "nianling" << q. front ( ) . m_Age << endl;
cout << "队尾元素--姓名:" << q. back ( ) . m_Name << "年龄:" << q. back ( ) . m_Age << endl;
q. pop ( ) ;
}
cout << "队列大小为:" << q. size ( ) << endl;
}
int main ( )
{
test01 ( ) ;
return 0 ;
}
二十八、list容器赋值和交换
/*list容器赋值和交换*/
#include <iostream>
using namespace std;
#include <list>
void printList(const list<int>&L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2;
L2 = L1;
printList(L2);
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int>L4;
L4.assign(10, 100);
printList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2;
L2.assign(10, 100);
cout << "交换前:" << endl;
printList(L1);
printList(L2);
L1.swap(L2);
cout << "交换后:" << endl;
printList(L1);
printList(L2);
}
int main()
{
test01();
test02();
return 0;
}
二十九、list容器的大小操作
#include <iostream>
using namespace std;
#include <list>
void printList(const list<int>&L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//判断容器是否为空
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的元素个数为:" << L1.size() << endl;
}
//重新指定大小
L1.resize(10,520);
printList(L1);
}
int main()
{
test01();
system("pause");
return 0;
三十、list容器的插入和删除
/*list容器的插入和删除*/
#include <iostream>
using namespace std;
#include <list>
void printList(const list<int>&L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//尾插
list<int>L;
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//insert插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//200 1000 100 10 20
//删除
it = L.begin();
L.erase(++it);
//200 100 10 20
printList(L);
//移除
L.push_back(10000);
L.push_back(10000);
printList(L);
L.remove(10000);
printList(L);
L.clear();
printList(L);
}
int main()
{
test01();
return 0;
}