STL----list

C++ list用法

创建一个list实例并赋值:

复制代码
 1 // 创建实例以及赋值
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     //第一种,通过构造函数
 7     int myints[] = {75,23,65,42,13};
 8     list<int> mylist1(myints, myints+5);
 9     list<int> mylist2(2,100);         // 2个值为100的元素
10     //第二种,用push_back,或push_front
11     for (int i = 1; i <= 5; ++i) mylist1.push_back(i);
12     mylist2.push_front (200);
13     mylist2.push_front (300);
14     //第三种,用assign
15     list<int> first;
16     list<int> second;
17     first.assign(7,100);                       // 给first添加7个值为100的元素
18     second.assign(first.begin(), first.end()); // 复制first给second
19     int myints[] = {16, 8, 4};
20     first.assign (myints, myints + 3);         // 将数组myints的内容添加给first
21 
22     //第四种,见insert函数
23     return 0;
24 }
复制代码

 

成员函数:

Iterator:  (可用于遍历list)

iterator begin();  //返回指向第一个元素的迭代器

iterator end();  //返回指向最后一个元素的迭代器

reverse_iterator rbegin();  //返回指向第一个元素的逆向迭代器

reverse_rend();  //返回指向最后一个元素的逆向迭代器

复制代码
 1 //list的遍历
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     int myints[] = {75,23,65,42,13};
 7     list<int> mylist (myints,myints+5);
 8     cout << "mylist contains:";
 9     //这是正序输出:
10     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
11         cout << ' ' << *it;
12     cout << '\n';
13 
14     list.clear();
15     //逆序输出:
16     for (int i = 1; i <= 5; ++i) mylist.push_back(i);
17     cout << "mylist backwards:";
18     for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)
19         cout << ' ' << *rit;
20     cout << '\n';
21     return 0;
22 }

输出结果为:
mylist contains: 75 23 65 42 13
mylist backwards: 5 4 3 2 1
复制代码

 

Capacity: (用于获取list容器大小信息)

bool empty() const;  //list为空时返回true

size_type size() const;  //返回list容器里元素的个数

size_type max_size() const;  //返回list容器最大能容纳的元素的个数,主要用于调用list的resize()函数时,检查所请求的size大小是否被允许

Element access:(用于获取首尾元素)

reference front();  //返回第一个元素的引用

const_reference front() const;

reference back();  //返回最后一个元素的引用

const_reference front() const;

Modifiers:

  • asign  //给容器添加新内容:

template<class InputIterator>

void assign(InputIterator first, InputIterator last);  //first,last是一个序列中起始和结束的迭代器的值,[first, last)包含了序列中所有元素

void assign(size_type n, const value_type& val);  //给list赋值n个值为val的元素

复制代码
 1 // list::assign
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> first;
 7     list<int> second;
 8     first.assign(7,100);                      // 给first添加7个值为100的元素
 9     second.assign(first.begin(), first.end()); // 复制first给second
10 
11     int myints[] = {16, 8, 4};
12     first.assign (myints, myints + 3);            // 将数组myints的内容添加给first
13 
14     cout << "Size of first: " << int (first.size()) << '\n';
15     cout << "Size of second: " << int (second.size()) << '\n';
16     return 0;
17 }
输出结果为:
Size of first: 3
Size of second: 7
复制代码
  • push_front, pop_front, push_back, pop_back

void push_front(const value_type& val);  //在list头添加元素

void pop_front();  //删除list头的元素

void push_back(const value_type& val);  //在list尾添加元素

void pop_back();  //删除list尾的元素

复制代码
 1 #include <iostream>
 2 #include <list>
 3 using namespace std;
 4 int main () {
 5     list<int> mylist (2,100);         // 2个值为100的元素
 6     // list::push_front
 7     mylist.push_front (200);
 8     mylist.push_front (300);
 9 
10     cout << "mylist contains:";
11     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
12         cout << ' ' << *it;
13     cout << '\n';
14 
15     // list::pop_front
16     cout << "Popping out the elements in mylist:";
17     while (!mylist.empty()) {
18         cout << ' ' << mylist.front();
19         mylist.pop_front();
20     }
21     cout << "\nFinal size of mylist is " << mylist.size() << '\n';
22 
23     // list::push_back
24     int myint;
25     cout << "Please enter some integers (enter 0 to end):\n";
26     do {
27         cin >> myint;
28         mylist.push_back (myint);
29     } while (myint);
30     cout << "mylist contains:";
31     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
32         cout << ' ' << *it;
33     cout << '\n';
34 
35     // list::pop_back
36     while (!mylist.empty()) {
37         cout << ' ' <<  mylist.back();
38         mylist.pop_back();
39     }
40     cout << "\nFinal size of mylist is " << mylist.size() << '\n';
41 
42     return 0;
43 }
输出结果:
mylist contai: 300 200 100 100
Popping out the elements in mylist: 300 200 100 100
Final size of mylist is 0
Please enter some integers (enter 0 to end):
56 23 8 5 6 0
mylist contains: 56 23 8 5 6 0
 0 6 5 8 23 56
Final size of mylist is 0
复制代码
  • insert  //插入元素:

iterator insert (iterator position, const value_type& val);  //position是要插入的这个list的迭代器,val是要插入的值

void insert (iterator position, size_type n, const value_type& val);  //从该list容器中的position位置处开始,插入n个值为val的元素

template <class InputIterator>

void insert (iterator position, InputIterator first, InputIterator last);  //first,last是我们选择的把值插入到这个list中的值所在的容器的迭代器

复制代码
 1 // inserting into a list
 2 #include <iostream>
 3 #include <list>
 4 #include <vector>
 5 using namespace std;
 6 int main () {
 7     list<int> mylist;
 8     list<int>::iterator it;
 9     // 初始化
10     for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5
11     it = mylist.begin();
12     ++it;       // 迭代器it现在指向数字2                      ^
13     //在i0t指向的位置出插入元素10
14     mylist.insert (it,10);                        // 1 10 2 3 4 5
15 
16     // "it" 仍然指向数字2                                   ^
17     //在it指向的位置出插入两个元素20
18     mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5
19 
20     --it;       // 现在it指向数字20                             ^
21 
22     vector<int> myvector (2,30); //创建vector容器,并初始化为含有2个值为30的元素
23     //将vector容器的值插入list中
24     mylist.insert (it,myvector.begin(),myvector.end());
25                                                 // 1 10 20 30 30 20 2 3 4 5
26     //it仍然指向数字20                            //               ^
27     cout << "mylist contains:";
28     for (it = mylist.begin(); it != mylist.end(); ++it)
29         cout << ' ' << *it;
30     cout << '\n';
31 
32     return 0;
33 }
输出结果:
mylist contains: 1 10 20 30 30 20 2 3 4 5
复制代码

 

  • erase  //删除元素:

iterator erase (iterator position);  //删除迭代器position指向的值,也可以不用变量接收其返回值

iterator erase (iterator first, iterator last);  //删除[first, last)中的值,也可以不用变量接收其返回值

复制代码
 1 // erasing from list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     list<int>::iterator it1,it2;
 8 
 9     // set some values:
10     for (int i = 1; i < 10; ++i) mylist.push_back(i*10);
11 
12                                 // 10 20 30 40 50 60 70 80 90
13     it1 = it2 = mylist.begin(); // ^^
14     advance (it2,6);            // ^                 ^
15     ++it1;                      //    ^              ^
16 
17     it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
18                                 //    ^           ^
19 
20     it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
21                                 //    ^           ^
22 
23     ++it1;                      //       ^        ^
24     --it2;                      //       ^     ^
25     //没有变量接收其返回值
26     mylist.erase (it1,it2);     // 10 30 60 80 90
27                                 //       ^
28     cout << "*it1 : " << *it1 << endl;
29     cout << "mylist contains:";
30     for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
31         cout << ' ' << *it1;
32     cout << '\n';
33 
34   return 0;
35 }
输出结果:
it1 : 40
mylist contains: 10 30 60 80 90
复制代码

 

  • swap  //交换两个list的内容

void swap(list& x);  //要交换的两个列表的存储的元素的类型必须是一样的,列表大小可以不同

复制代码
 1 // swap lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> first (3,100);   // 三个值为100的元素
 7     list<int> second (5,200);  // 五个值为200的元素
 8 
 9     first.swap(second);
10 
11     cout << "first contains:";
12     for (list<int>::iterator it = first.begin(); it != first.end(); it++)
13         cout << ' ' << *it;
14     cout << '\n';
15 
16     cout << "second contains:";
17     for (list<int>::iterator it = second.begin(); it != second.end(); it++)
18         cout << ' ' << *it;
19     cout << '\n';
20 
21     return 0;
22 }
输出结果:
first contains: 200 200 200 200 200
second contains: 100 100 100
复制代码

 

  • resize  //调整list大小

void resize (size_type n, value_type val = value_type());  //将list大小调整为能容纳n个元素,若n大于当前list大小,则会从list末尾一直插入val值,直到list大小满足n;

复制代码
 1 // resizing list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7 
 8     // 初始化
 9     for (int i = 1; i < 10; ++i) mylist.push_back(i);
10 
11     mylist.resize(5);
12     mylist.resize(8,100);
13     mylist.resize(12);
14 
15     cout << "mylist contains:";
16     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
17         cout << ' ' << *it;
18     cout << '\n';
19 
20   return 0;
21 }
输出结果:
mylist contains: 1 2 3 4 5 100 100 100 0 0 0 0
复制代码

 

  • clear  //清空list

void clear();  //删除list的所有元素

复制代码
 1 // clearing lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     list<int>::iterator it;
 8 
 9     mylist.push_back (100);
10     mylist.push_back (200);
11     mylist.push_back (300);
12 
13     cout << "mylist contains:";
14     for (it = mylist.begin(); it != mylist.end(); ++it)
15         cout << ' ' << *it;
16     cout << '\n';
17 
18     mylist.clear();
19     mylist.push_back (1101);
20     mylist.push_back (2202);
21 
22     cout << "mylist contains:";
23     for (it = mylist.begin(); it != mylist.end(); ++it)
24         cout << ' ' << *it;
25     cout << '\n';
26 
27   return 0;
28 }
输出结果:
mylist contains: 100 200 300
mylist contains: 1101 2202
复制代码

 

Operations:

  • splice  //将一个list中的值移到另一个list中

void splice (iterator position, list& x);  //将列表x中的所有元素移到当前list中,从当前列表的position指向的位置开始,此时列表x为空

void splice (iterator position, list& x, iterator i);  //将列表x中迭代器 i 指向的元素移到当前list的position指向的位置处,由于i指向的元素从列表x中被移除,所以迭代器 i 此时是invalid的;position是当前列表的迭代器,i是列表x的迭代器

void splice (iterator position, list& x, iterator first, iterator last);  //将列表x中[first, last)的元素移到当前list中,从position指向的位置开始;first, last是列表x的迭代器

复制代码
 1 // splicing lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist1, mylist2;
 7     list<int>::iterator it;
 8 
 9     // 初始化
10     for (int i = 1; i <= 4; ++i)
11        mylist1.push_back(i);      // mylist1: 1 2 3 4
12 
13     for (int i = 1; i <= 3; ++i)
14        mylist2.push_back(i*10);   // mylist2: 10 20 30
15 
16     it = mylist1.begin();
17     ++it;                         // 指向数字2
18 
19     mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
20                                   // mylist2 (empty)
21                                   // "it" 仍然指向数字2
22 
23     mylist2.splice (mylist2.begin(),mylist1, it);
24                                 // mylist1: 1 10 20 30 3 4
25                                 // mylist2: 2
26                                 // "it" 此时已经无效了
27     it = mylist1.begin();
28     advance(it,3);           // "it" 指向数字30
29 
30     mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
31                                 // mylist1: 30 3 4 1 10 20
32 
33     cout << "mylist1 contains:";
34     for (it = mylist1.begin(); it != mylist1.end(); ++it)
35         cout << ' ' << *it;
36     cout << '\n';
37 
38     cout << "mylist2 contains:";
39     for (it = mylist2.begin(); it != mylist2.end(); ++it)
40         cout << ' ' << *it;
41     cout << '\n';
42 
43   return 0;
44 }
输出结果:
mylist1 contains: 30 3 4 1 10 20
mylist2 contains: 2
复制代码

 

  • remove  //删除list中特定的值

void remove (const value_type& val);  //从list中删除所有值为val的元素

复制代码
 1 // remove from list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     int myints[]= {17, 89, 7, 89, 14};
 7     list<int> mylist (myints,myints+5);
 8 
 9     mylist.remove(89);
10 
11     cout << "mylist contains:";
12     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
13         cout << ' ' << *it;
14     cout << '\n';
15 
16     return 0;
17 }
输出结果:
mylist contains: 17 7 14
复制代码

 

  • remove_if  //按条件删除

template <class Predicate>

void remove_if (Predicate pred);  //pred可以是一个函数,也可以是一个class,但它需要有一个参数,且参数类型跟list中存储元素类型相同,满足条件就返回true

复制代码
 1 // list::remove_if
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 // a predicate implemented as a function:
 6 bool single_digit (const int& value) { return (value < 10); }
 7 
 8 // a predicate implemented as a class:
 9 struct is_odd {
10   //重载操作符 ()
11   bool operator() (const int& value) { return (value % 2) == 1; }
12 };
13 
14 int main () {
15     int myints[] = {15, 36, 7, 17, 20, 39, 4, 1};
16     list<int> mylist (myints, myints + 8);   // 15 36 7 17 20 39 4 1
17 
18     mylist.remove_if (single_digit);           // 15 36 17 20 39
19 
20     mylist.remove_if (is_odd());               // 36 20
21 
22     cout << "mylist contains:";
23     for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
24         cout << ' ' << *it;
25     cout << '\n';
26 
27     return 0;
28 }
输出结果:
mylist contains: 36 20
复制代码

 

  • unique  //删除重复值

void unique();  //只能删除相邻的重复元素,然后保留第一个值,因此这个函数只对排好序的list有用

template <class BinaryPredicate>

void unique (BinaryPredicate binary_pred);  //binary_pred可以是函数,也可以是class,但它需要有两个参数,且类型跟list中存储的值类型相同,满足某个条件就返回true

复制代码
 1 // list::unique
 2 #include <iostream>
 3 #include <cmath>
 4 #include <list>
 5 using namespace std;
 6 // a binary predicate implemented as a function:
 7 bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); }
 8 
 9 // a binary predicate implemented as a class:
10 struct is_near {
11     bool operator() (double first, double second) { return (fabs(first-second)<5.0); }
12 };
13 
14 int main () {
15     double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
16                        12.77, 73.35, 72.25, 15.3, 72.25 };
17     list<double> mylist (mydoubles,mydoubles+10);
18     cout << "mylist contains:";
19     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
20         cout << ' ' << *it;
21     cout << '\n';
22     
23     mylist.unique();
24     cout << "mylist contains:";
25     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
26         cout << ' ' << *it;
27     cout << '\n';
28 
29     mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
30                              // 15.3,  72.25, 72.25, 73.0,  73.35
31 
32     mylist.unique();           //  2.72,  3.14, 12.15, 12.77
33                              // 15.3,  72.25, 73.0,  73.35
34     cout << "mylist contains:";
35     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
36         cout << ' ' << *it;
37     cout << '\n';
38 
39     mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15
40                                        // 15.3,  72.25, 73.0
41     cout << "mylist contains:";
42     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
43         cout << ' ' << *it;
44     cout << '\n';
45 
46     mylist.unique (is_near());           //  2.72, 12.15, 72.25
47 
48     cout << "mylist contains:";
49     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
50         cout << ' ' << *it;
51     cout << '\n';
52 
53   return 0;
54 }
输出结果:
mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25 mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25 mylist contains: 2.72 3.14 12.15 12.77 15.3 72.25 73 73.35
mylist contains: 2.72 3.14 12.15 15.3 72.25 73
mylist contains: 2.72 12.15 72.25
 
      
复制代码

 

  • merge  //合并有序的list

void merge(list &x);  //会将列表x中的元素按默认的顺序移入当前列表当中,此时列表x为空,当前列表仍为有序列表

template<class Compare>

void merge (list& x, Compare comp);  //comp可以为一个函数,要求有两个参数且参数类型跟list中存储的元素类型相同,当满足条件时返回true,merge就按照这个条件将两个列表合并

复制代码
 1 // list::merge
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 // compare only integral part:
 6 bool mycomparison (double first, double second) { return ( (first)<(second) ); }
 7 
 8 int main () {
 9     list<double> first, second;
10 
11     first.push_back (3.1);
12     first.push_back (2.2);
13     first.push_back (2.9);
14 
15     second.push_back (3.7);
16     second.push_back (7.1);
17     second.push_back (1.4);
18 
19     first.sort();
20     second.sort();
21 
22     first.merge(second);
23     cout << "first contains:";
24     for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
25         cout << ' ' << *it;
26     cout << '\n';
27     // (second 现在为空)
28 
29     second.push_back (2.1);
30     second.push_back(2.5);
31 
32     first.merge(second,mycomparison);
33     cout << "first contains:";
34     for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
35         cout << ' ' << *it;
36     cout << '\n';
37 
38   return 0;
39 }
输出结果:
first contains: 1.4 2.2 2.9 3.1 3.7 7.1
first contains: 1.4 2.1 2.2 2.5 2.9 3.1 3.7 7.1

复制代码

 

  • sort  //排序

void sort();  //默认升序排列

template <class Compare>

void sort (Compare comp);  //comp可以是一个函数,要求有两个参数,类型跟list中元素类型相同,满足条件时返回true,sort()函数就按照comp中制定的规则对元素进行排序

复制代码
 1 // list::sort
 2 #include <iostream>
 3 #include <list>
 4 #include <string>
 5 #include <cctype>
 6 using namespace std;
 7 // comparison, not case sensitive.
 8 bool compare_nocase (const string& first, const string& second) {
 9     unsigned int i = 0;
10     while ((i < first.length()) && (i < second.length()) ) {
11         //将大写字母转为小写字母
12         if (tolower(first[i]) < tolower(second[i])) return true;
13         else if (tolower(first[i]) > tolower(second[i])) return false;
14         ++i;
15     }
16     return ( first.length() < second.length() );
17 }
18 
19 int main () {
20     list<string> mylist;
21     list<string>::iterator it;
22     mylist.push_back ("one");
23     mylist.push_back ("two");
24     mylist.push_back ("Three");
25 
26     mylist.sort();
27 
28     cout << "mylist contains:";
29     for (it = mylist.begin(); it != mylist.end(); ++it)
30         cout << ' ' << *it;
31     cout << '\n';
32 
33     mylist.sort(compare_nocase);
34 
35     cout << "mylist contains:";
36     for (it = mylist.begin(); it != mylist.end(); ++it)
37         cout << ' ' << *it;
38     cout << '\n';
39 
40   return 0;
41 }
输出结果:
mylist contains: Three one two
mylist contains: one Three two
复制代码

 

  • reverse  //逆序:

void reverse();  //将list中元素的顺序逆转过来

复制代码
 1 // reversing list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7 
 8     for (int i = 1; i < 10; ++i) mylist.push_back(i);
 9 
10     mylist.reverse();
11 
12     cout << "mylist contains:";
13     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
14         cout << ' ' << *it;
15     cout << '\n';
16 
17   return 0;
18 }
输出结果:
mylist contains: 9 8 7 6 5 4 3 2 1
复制代码

 

Observers:

  • get_allocator  //返回一个跟该list有关的分配器对象

allocator_type get_allocator() const;  //可以用来给数组动态分配空间

复制代码
 1 // list::get_allocator
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     int * p;
 8 
 9     // allocate an array of 5 elements using mylist's allocator:
10     p = mylist.get_allocator().allocate(5);
11 
12     // assign some values to array
13     for (int i = 0; i < 5; ++i) p[i] = i;
14 
15     cout << "The allocated array contains:";
16     for (int i = 0; i < 5; ++i) cout << ' ' << p[i];
17     cout << '\n';
18 
19     mylist.get_allocator().deallocate(p,5);
20 
21     return 0;
22 }
输出结果:
The allocated array contains: 9 8 7 6 5 4 3 2 1
复制代码

 

标签: list
Author:

声明:来自转载,已有声明,尊重原创,如有冒犯,联系删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值