参考资料:http://www.cplusplus.com/reference/list/list/
一、常用API
1,构造函数
std::list<int> first;
std::list<int> second (4,100); // (100,100,100,100)
std::list<int> third (second.begin(),second.end());
std::list<int> fourth (third);
int myints[] = {16,2,77,29};
std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); //(16,2,77,29)
2,查找操作
【back】返回list的最后一个元素值 mylist.back()
【front】返回list的第一个元素值 mylist.front()
3,插入操作
【emplace】迭代器指定位置插入元素std::list< std::pair<int,char> > mylist; mylist.emplace ( mylist.begin(), 100, 'x' );
【emplace_back】在list末尾插入元素 std::list< std::pair<int,char> > mylist; mylist.emplace_back(10,'a');
【emplace_front】在list首部插入元素std::list< std::pair<int,char> > mylist; mylist.emplace_front(10,'a');
【inser】在迭代器指定位置处插入元素,迭代器所指的值不会变化
PS: list<int> mylist = {1,2,3}; list<int>::iterator it = mylist.begin(); ++it; //it 指向2
mylist.insert (it,10);//2前插入10{1,10,2,3} mylist.insert (it,2,20);//it扔指向2,2前插入2个20{1,10,20,20,2,3}
【push_front】列表首部插入一个元素mylist1.push_front(value)
【push_back】列表尾部追加一个元素mylist1.push_back(value)
4,删除操作
【clear】清除list中所有元素,使其长度为0 mylist.clear()
【erase】清除迭代器指定的当前元素,返回指向被删元素的下一个元素的迭代器it=mylist.erase(it)
PS:for(list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it) mylist.erase(it)这样是错误的,erase之后it失效,需赋值;
但,for(list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it) it=mylist.erase(it)也不会将mylist中的所有元素清除
【pop_back】删除列表中最后一个元素mylist.pop_back()
【pop_front】删除列表中第一个元素mylist.pop_front()
【remove】删除列表中所有值为指定的值的元素 mylist.remove(value)
【remove_if】删除列表中满足条件的元素,参数可以是函数,也可以是结构体
PS:bool single_digit (const int& value) { return (value<10); } mylist.remove_if (single_digit);
struct is_odd { bool operator() (const int& value) { return (value%2)==1; }}; mylist.remove_if (is_odd());
5,迭代器
【begin, end】【rbegin,rend】 std::list<int>::iterator it;std::list<int>::reverse_iterator rit
【cbegin,cend】【crbegin,crend】 std::list<int>::const_iterator it ; std::list<int>::const_reverse_iterator rit
6,赋值操作
【assign】
mylist1.assign (4,100); //(100,100,100,100)
mylist2.assign (mylist1.begin(),mylist1.end());
int myints[]={1776,7,4};
mylist1.assign (myints,myints+3);
7,存储空间大小
【empty】判断list是否为空,返回true|false , mylist.empty()
【size】返回list的元素个数
【max_size】list能容纳的最大元素个数
【resize】使list的长度为指定长度,不足用val不足,足的截断 mylist.resize(n) mylist.resize(n, val)
8,排序,合并,反转操作
【sort】排序,默认增序 mylist1.sort() mylist1.sort(compare_nocase)
eg,mylist1={"one","two","Three"}
// 比较函数,不区分大小写
bool compare_nocase (const std::string& first, const std::string& second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
return ( first.length() < second.length() );
}
mylist.sort(); ----> (Three, one,two)
mylist.sort(compare_nocase); --->(one, Three ,two)
【merge】合并排序的列表 mylist1.merge(mylist2) 将mylist2各个元素合并至有序的mylist1中,mylist2变空
PS:可自定义排序函数 mylist1.merge(mylist2, mycomparion)
bool mycomparison (double first, double second){ return ( int(first)<int(second) ); } //升序,但只比较double类型的整数部分
【reverse】反转列表 mylist1.reverse()
【splice】从list1传输元素到list2
PS:
mylist1.splice(iteration, mylist2) 将mylist2中的所有元素转到mylist1中it所指向的位置,it指向的位置不变
mylist1.splice(iteration1, mylist2, iteration2) 将mylist2中iteration2所指向的元素放至mylist1的iteration1所指位置,iteration2失效
mylist1.splice(iteration1, mylist2, iteration2_start, iteration2_end) 将mylist2中[it2_start, it2_end)范围内的元素放至mylist1中it1所指位置
【swap】交换两个列表元素 mylist1.swap(mylist2)
【unique】删除重复的值
PS:
mylist1.unique() 删除容器中每个连续的相等元素,除了第一个元素之外的所有元素。
注意,如果一个元素的值等于它前面的元素,那么它只会从列表容器中删除。 因此,此函数对排序列表(mylist1.sort())特别有用。
mylist1.unique(same_integral) ; mylist1.unique(is_near()) 将参数作为确定元素的“唯一性”的特定比较函数,从第二个元素开始,与前面元素进行比较
bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); }
struct is_near { bool operator() (double first, double second) { return (fabs(first-second)<5.0); } };