构造函数
-
default (1)
explicit list (const allocator_type& alloc = allocator_type());
-
fill (2)
explicit list (size_type n);
list (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
-
range (3)
template <class InputIterator>
list (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
-
copy (4)
list (const list& x);
list (const list& x, const allocator_type& alloc);
-
move (5)
list (list&& x);
list (list&& x, const allocator_type& alloc);
-
initializer list (6)
list (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
赋值运算符
-
copy (1)
list& operator= (const list& x);
-
move (2)
list& operator= (list&& x);
-
initializer list (3)
list& operator= (initializer_list<value_type> il);
迭代器

容量

访问元素

修改

assign函数
将新内容分配给列表容器,替换其当前内容,并相应地修改其大小。
-
range (1)
template <class InputIterator>
void assign (InputIterator first, InputIterator last);
-
fill (2)
void assign (size_type n, const value_type& val);
-
initializer list (3)
void assign (initializer_list<value_type> il);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> first;
std::list<int> second;
first.assign (7,100); // 7 ints with value 100
second.assign (first.begin(),first.end()); // a copy of first
int myints[]={1776,7,4};
first.assign (myints,myints+3); // assigning from array
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
return 0;
}
|
emplace_front函数
void emplace_front(Args && ... args);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
#include <list>
int main ()
{
std::list< std::pair<int,char> > mylist;
mylist.emplace_front(10,'a');
mylist.emplace_front(20,'b');
mylist.emplace_front(30,'c');
std::cout << "mylist contains:";
for (auto& x: mylist)
std::cout << " (" << x.first << "," << x.second << ")";
std::cout << std::endl;
return 0;
}
|
push_front函数
void push_front (const value_type& val);
void push_front (value_type&& val);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist (2,100); // two ints with a value of 100
mylist.push_front (200);
mylist.push_front (300);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
pop_front函数
void pop_front();
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
mylist.push_back (100);
mylist.push_back (200);
mylist.push_back (300);
std::cout << "Popping out the elements in mylist:";
while (!mylist.empty())
{
std::cout << ' ' << mylist.front();
mylist.pop_front();
}
std::cout << "\nFinal size of mylist is " << mylist.size() << '\n';
return 0;
}
|
emplace_back函数
template <class... Args>
void emplace_back (Args&&... args);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
#include <list>
int main ()
{
std::list< std::pair<int,char> > mylist;
mylist.emplace_back(10,'a');
mylist.emplace_back(20,'b');
mylist.emplace_back(30,'c');
std::cout << "mylist contains:";
for (auto& x: mylist)
std::cout << " (" << x.first << "," << x.second << ")";
std::cout << std::endl;
return 0;
}
|
##push_back函数
void push_back (const value_type& val);
void push_back (value_type&& val);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
int myint;
std::cout << "Please enter some integers (enter 0 to end):\n";
do {
std::cin >> myint;
mylist.push_back (myint);
} while (myint);
std::cout << "mylist stores " << mylist.size() << " numbers.\n";
return 0;
}
|
##pop_back函数
void pop_back();
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
int sum (0);
mylist.push_back (100);
mylist.push_back (200);
mylist.push_back (300);
while (!mylist.empty())
{
sum+=mylist.back();
mylist.pop_back();
}
std::cout << "The elements of mylist summed " << sum << '\n';
return 0;
}
|
emplace函数
template <class... Args>
iterator emplace (const_iterator position, Args&&... args);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
#include <list>
int main ()
{
std::list< std::pair<int,char> > mylist;
mylist.emplace ( mylist.begin(), 100, 'x' );
mylist.emplace ( mylist.begin(), 200, 'y' );
std::cout << "mylist contains:";
for (auto& x: mylist)
std::cout << " (" << x.first << "," << x.second << ")";
std::cout << '\n';
return 0;
}
|
insert函数
-
single element (1)
iterator insert (const_iterator position, const value_type& val);
-
fill (2)
iterator insert (const_iterator position, size_type n, const value_type& val);
-
range (3)
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
-
move (4)
iterator insert (const_iterator position, value_type&& val);
-
initializer list (5)
iterator insert (const_iterator position, initializer_list<value_type> il);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <iostream>
#include <list>
#include <vector>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
std::vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
erase函数
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it1,it2;
// set some values:
for (int i=1; i<10; ++i) mylist.push_back(i*10);
// 10 20 30 40 50 60 70 80 90
it1 = it2 = mylist.begin(); // ^^
advance (it2,6); // ^ ^
++it1; // ^ ^
it1 = mylist.erase (it1); // 10 30 40 50 60 70 80 90
// ^ ^
it2 = mylist.erase (it2); // 10 30 40 50 60 80 90
// ^ ^
++it1; // ^ ^
--it2; // ^ ^
mylist.erase (it1,it2); // 10 30 60 80 90
// ^
std::cout << "mylist contains:";
for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
std::cout << ' ' << *it1;
std::cout << '\n';
return 0;
}
|
swap函数
void swap (list& x);
通过x来交换容器的内容,需要保证容器类型相同,大小可能不同。
调用此函数后,所有迭代器,引用和指针对交换对象仍然有效。
非成员函数中也有swap函数,该函数采用和本函数相似的算法优化.
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> first (3,100); // three ints with a value of 100
std::list<int> second (5,200); // five ints with a value of 200
first.swap(second);
std::cout << "first contains:";
for (std::list<int>::iterator it=first.begin(); it!=first.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "second contains:";
for (std::list<int>::iterator it=second.begin(); it!=second.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
resize函数
void resize(size_type n);
void resize(size_type n,const value_type&val);
调整容器大小,使其包含n个元素。
如果n小于当前的容器大小,则将内容减少到其前n个元素,删除超出(并且销毁它们)的元素。
如果n大于当前的容器大小,则通过根据需要插入许多要达到大小n的元素来扩展内容。如果指定了val,则新元素将被初始化为val的副本,否则它们被初始化。
请注意,此功能通过插入或删除其中的元素来更改容器的实际内容。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
// set some initial content:
for (int i=1; i<10; ++i) mylist.push_back(i);
mylist.resize(5);
mylist.resize(8,100);
mylist.resize(12);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
clear函数
void clear() noexcept;
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
mylist.push_back (100);
mylist.push_back (200);
mylist.push_back (300);
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
mylist.clear();
mylist.push_back (1101);
mylist.push_back (2202);
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
操作

splice函数
-
entire list (1)
void splice (const_iterator position, list& x);
void splice (const_iterator position, list&& x);
-
single element (2)
void splice (const_iterator position, list& x, const_iterator i);
void splice (const_iterator position, list&& x, const_iterator i);
-
element range (3)
void splice (const_iterator position, list& x,
const_iterator first, const_iterator last);
void splice (const_iterator position, list&& x,
const_iterator first, const_iterator last);
将元素从列表转移到列表
将元素从x转移到容器中,将它们插入位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
// splicing lists
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist1, mylist2;
std::list<int>::iterator it;
// set some initial values:
for (int i=1; i<=4; ++i)
mylist1.push_back(i); // mylist1: 1 2 3 4
for (int i=1; i<=3; ++i)
mylist2.push_back(i*10); // mylist2: 10 20 30
it = mylist1.begin();
++it; // points to 2
mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)
mylist2.splice (mylist2.begin(),mylist1, it);
// mylist1: 1 10 20 30 3 4
// mylist2: 2
// "it" is now invalid.
it = mylist1.begin();
std::advance(it,3); // "it" points now to 30
mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
// mylist1: 30 3 4 1 10 20
std::cout << "mylist1 contains:";
for (it=mylist1.begin(); it!=mylist1.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "mylist2 contains:";
for (it=mylist2.begin(); it!=mylist2.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
remove函数
void remove(const value_type&val);
删除具有特定值的元素
从容器中删除所有与val相等的元素。这调用这些对象的析构函数,并通过删除元素的数量减少容器大小。
与成员函数list :: erase不同的是,它通过它们的位置(使用迭代器)来清除元素,这个函数(list :: remove)通过它们的值去除元素。
存在一个类似的函数list :: remove_if,它允许除了等式比较之外的条件来确定元素是否被删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
#include <list>
int main ()
{
int myints[]= {17,89,7,14};
std::list<int> mylist (myints,myints+4);
mylist.remove(89);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
remove_if函数
template <class Predicate>
void remove_if (Predicate pred);
去除符合条件的元素
从容器中删除Predicate pred返回true的所有元素。这调用这些对象的析构函数,并通过删除元素的数量减少容器大小。
该函数为每个元素调用pred(* i)(其中i是该元素的迭代器)。列表中返回true的任何元素都将从容器中删除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// list::remove_if
#include <iostream>
#include <list>
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {
bool operator() (const int& value) { return (value%2)==1; }
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
std::list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
unique函数
(1)
void unique();
(2)
template <class BinaryPredicate>
void unique (BinaryPredicate binary_pred);
(1)从容器中的每个连续的相等元素组除去第一个元素之外的所有元素。
请注意,如果元素的比较等于其前面的元素,则该元素将从列表容器中删除。因此,此功能对排序列表特别有用。
(2)将一个特定的比较函数作为参数,确定元素的“唯一性”。实际上,任何行为都可以被实现(而不仅仅是一个等式的比较),但是注意到这个函数将调用binary_pred(* i,*(i-1))来获取所有的元素对(其中i是元素的迭代器) ,从第二个开始), 如果谓词返回true,则将i从列表中删除。 删除的元素被破坏。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <iostream>
#include <cmath>
#include <list>
// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }
// a binary predicate implemented as a class:
struct is_near {
bool operator() (double first, double second)
{ return (fabs(first-second)<5.0); }
};
int main ()
{
double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
std::list<double> mylist (mydoubles,mydoubles+10);
mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35
mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
mylist.unique (same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0
mylist.unique (is_near()); // 2.72, 12.15, 72.25
std::cout << "mylist contains:";
for (std::list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
merge函数
(1)
void merge (list& x);
void merge (list&& x);
(2)
template <class Compare>
void merge (list& x, Compare comp);
template <class Compare>
void merge (list&& x, Compare comp);
合并排序列表
通过将其各自的有序位置的所有元素转移到容器中(将两个容器都已经订购) 将x合并到列表中。
这有效地删除x中的所有元素(变为空),并将它们插入到容器内的有序位置(其大小通过传送的元素的数量进行扩展)。执行操作而不构建或破坏任何元素:它们被转移,无论x是值还是右值,还是value_type是否支持移动构造。
具有两个参数(2)的模板版本具有相同的行为,但采取特定的谓词( comp)来执行元素之间的比较操作。这种比较应该产生严格的元素的弱序列(即,一致的传递比较,而不考虑其反射性)。
此函数要求列表容器在调用之前已经按值(或comp)排序其元素。有关无序列表的替代方法,请参阅list :: splice。
假设这样排序,x的每个元素根据由operator <或comp定义的严格弱序列插入到与其值对应的位置。等效元素的结果顺序是稳定的(即等效元素保留调用之前的相对顺序,现有元素先于从x插入的等价元素)。(&x == this) 函数什么也不做。
sort函数
(1)
void sort();
(2)
template <class Compare>
void sort (Compare comp);
在容器中排序元素
对列表中的元素进行排序,改变它们在容器内的位置。
通过应用使用运算符<(在版本(1))或comp(在版本(2))中的比较元素来执行排序。这种比较应该产生严格的元素的弱序列(即,一致的传递比较,而不考虑其反射性)。
等效元素的结果顺序是稳定的:即,等效元素保留在调用之前拥有的相对顺序。
整个操作不涉及任何元素对象的构造,销毁或复制。元素在容器内移动。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
// list::sort
#include <iostream>
#include <list>
#include <string>
#include <cctype>
// comparison, not case sensitive.
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() );
}
int main ()
{
std::list<std::string> mylist;
std::list<std::string>::iterator it;
mylist.push_back ("one");
mylist.push_back ("two");
mylist.push_back ("Three");
mylist.sort();
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
mylist.sort(compare_nocase);
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
reverse函数
void reverse()noexcept;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist;
for (int i=1; i<10; ++i) mylist.push_back(i);
mylist.reverse();
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
分配器

重载
(1)
template <class T, class Alloc>
bool operator== (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
(2)
template <class T, class Alloc>
bool operator!= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
(3)
template <class T, class Alloc>
bool operator< (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
(4)
template <class T, class Alloc>
bool operator<= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
(5)
template <class T, class Alloc>
bool operator> (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
(6)
template <class T, class Alloc>
bool operator>= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
#include <list>
int main ()
{
std::list<int> a = {10, 20, 30};
std::list<int> b = {10, 20, 30};
std::list<int> c = {30, 20, 10};
if (a==b) std::cout << "a and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
return 0;
}
|
swap函数
template <class T, class Alloc>
void swap (list<T,Alloc>& x, list<T,Alloc>& y);
所有迭代器,引用和指针对交换对象仍然有效。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
#include <list>
main ()
{
unsigned int i;
std::list<int> foo (3,100); // three ints with a value of 100
std::list<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (std::list<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (std::list<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|