list头文件:
#include <list>
using namespace std;
插入push_back,push_front,insert
删除pop_back,pop_front,erase,clear
遍历begin,end
判空empt
list的常用接口:
判空 empty
empty
语法:
|
empty()函数返回真(true)如果链表为空,否则返回假。例如:
list<int> the_list;
for( int i = 0; i < 10; i++ )
the_list.push_back( i );
while( !the_list.empty() ) {
cout << the_list.front() << endl;
the_list.pop_front();
}
获取list中元素的个数 size
size
语法:
|
size()函数返回list中元素的数量。
添加 push_back push_front insert
在链表的尾部插入元素: push_back
push_back
语法:
|
push_back()将val连接到链表的最后。例如:
list<int> the_list;
for( int i = 0; i < 10; i++ ){
the_list.push_back( i );
}
在链表的首部插入元素: push_front
push_front
语法:
|
push_front()函数将val连接到链表的头部。
在链表的指定位置上插入元素: insert
insert
语法:
iterator insert( iterator pos, const TYPE &val );
void insert( iterator pos, size_type num, const TYPE &val );
void insert( iterator pos, input_iterator start, input_iterator end );
insert()插入元素val到位置pos,或者插入num个元素val到pos之前,或者插入start到end之间的元素到pos的位置。返回值是一个迭代器,指向被插入的元素。
示例代码:
// list_class_insert.cpp
// compile with: /EHsc
#include <list>
#include <iostream>
#include <string>
int main( )
{
using namespace std;
list <int> c1, c2;
list <int>::iterator Iter;
c1.push_back( 10 );
c1.push_back( 20 );
c1.push_back( 30 );
c2.push_back( 40 );
c2.push_back( 50 );
c2.push_back( 60 );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
Iter = c1.begin( );
Iter++;
c1.insert( Iter, 100 );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
Iter = c1.begin( );
Iter++;
Iter++;
c1.insert( Iter, 2, 200 );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
c1.insert( ++c1.begin( ), c2.begin( ),--c2.end( ) );
cout << "c1 =";
for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
cout << " " << *Iter;
cout << endl;
// initialize a list of strings by moving
list < string > c2;
string str("a");
c2.insert( c2.begin(), move( str ) );
cout << "Moved first element: " << c2.front( ) << endl;
}
Output
c1 = 10 20 30 c1 = 10 100 20 30 c1 = 10 100 200 200 20 30 c1 = 10 40 50 100 200 200 20 30 Moved first element: a
删 除 pop_back pop_front erase clear
删除链表的最后一个元素,但不返回链表最后一个元素的值: pop_back
pop_back
语法:
|
pop_back()函数删除链表的最后一个元素。
删除链表首部的元素,但不返回链表第一个元素的值: pop_front
pop_front
语法:
|
pop_front()函数删除链表的第一个元素。
删除链表指定位置上的元素: erase
erase
语法:
|
erase()函数删除以pos指示位置的元素, 或者删除start和end之间的元素。 返回值是一个迭代器,指向最后一个被删除元素的下一个元素。
删除链表中的所有元素 clear
clear
语法:
|
clear()函数删除list的所有元素。
获取链表最后一个元素back 和 第一个元素front
获取list的最后一个元素,但不删除最后一个元素:
back
语法:
|
back()函数返回一个引用,指向list的最后一个元素。
获取list的第一个元素,但不删除第一个元素:
front
语法:
|
front()函数返回一个引用,指向链表的第一个元素。
list<int> the_list;
for( int i = 0; i < 10; i++ )
the_list.push_back( i );
while( !the_list.empty() ) {
cout << the_list.front() << endl;
the_list.pop_front();
}
遍历链表 begin end
获取指向list第一个元素的迭代器:
begin
语法:
|
begin()函数返回一个迭代器,指向list的第一个元素。例如,
// 创建一个元素类型是字符的链表
list<char> charList;
for( int i=0; i < 10; i++ )
charList.push_front( i + 65 );
// 显示这个链表
list<char>::iterator theIterator;
for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ )
cout << *theIterator;
获取指向list末尾的迭代器:
end
语法:
|
end()函数返回一个迭代器,指向链表的末尾。
链表合并 merge
merge
语法:
|
merge()函数把自己和lst链表连接在一起,产生一个整齐排列的组合链表。如果指定compfunction,则将指定函数作为比较的依据。
将list中的元素逆序 reverse
reverse
语法:
|
reverse()函数把list所有元素倒转。
对list中的元素进行排序 sort
排序(sort)
语法:
|
sort()函数为链表排序,默认是升序。如果指定compfunction的话,就采用指定函数来判定两个元素的大小。
修改链表的长度 resize
修改链表的大小,多余元素用val填充: resize
resize
语法:
|
resize()函数把list的大小改变到num。被加入的多余的元素都被赋值为val
删除链表中的重复元素 unique
unique
语法:
|
unique()函数删除链表中所有重复的元素。如果指定pr,则使用pr来判定是否删除。
---------------------------------------------------------------------------------------------------------------------------------------------------------
list和vector的不同在于,vector采用的顺序存储,所以vector可以进行随机存取。
而list则不一样。list采用链式存储方法,所以list不能随机存取,但是在list插入和删除元素的代价都非长笑。