C++ list容器

1、创建一个list实例并赋值

// 创建实例以及赋值

#include <iostream>

#include <list>

using namespace std;

int main () {

    //第一种,通过构造函数

    int myints[] = {75,23,65,42,13};

    list<int> mylist1(myints, myints+5);

    list<int> mylist2(2,100);         // 2个值为100的元素

    //第二种,用push_back,或push_front

    for (int i = 1; i <= 5; ++i) mylist1.push_back(i);

    mylist2.push_front (200);

    mylist2.push_front (300);

    //第三种,assign

    list<int> first;

    list<int> second;

    first.assign(7,100);                       // first添加7个值为100的元素

    second.assign(first.begin(), first.end()); // 复制firstsecond

    int myints[] = {16, 8, 4};

    first.assign (myints, myints + 3);         // 将数组myints的内容添加给first

 

    //第四种,见insert函数

    return 0;

}

2、成员函数

Iterator:  (可用于遍历list

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

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

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

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

//list的遍历

#include <iostream>

#include <list>

using namespace std;

int main () {

    int myints[] = {75,23,65,42,13};

    list<int> mylist (myints,myints+5);

    cout << "mylist contains:";

    //这是正序输出:

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    list.clear();

    //逆序输出:

    for (int i = 1; i <= 5; ++i) mylist.push_back(i);

    cout << "mylist backwards:";

    for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)

        cout << ' ' << *rit;

    cout << '\n';

    return 0;

}

 

输出结果为:

mylist contains: 75 23 65 42 13

mylist backwards: 5 4 3 2 1

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

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

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

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

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

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

const_reference front() const;

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

const_reference front() const;

5Modifiers:

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的元素

// list::assign

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> first;

    list<int> second;

    first.assign(7,100);                      // first添加7个值为100的元素

    second.assign(first.begin(), first.end()); // 复制firstsecond

 

    int myints[] = {16, 8, 4};

    first.assign (myints, myints + 3);            // 将数组myints的内容添加给first

 

    cout << "Size of first: " << int (first.size()) << '\n';

    cout << "Size of second: " << int (second.size()) << '\n';

    return 0;

}

输出结果为:

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尾的元素

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist (2,100);         // 2个值为100的元素

    // list::push_front

    mylist.push_front (200);

    mylist.push_front (300);

 

    cout << "mylist contains:";

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    // list::pop_front

    cout << "Popping out the elements in mylist:";

    while (!mylist.empty()) {

        cout << ' ' << mylist.front();

        mylist.pop_front();

    }

    cout << "\nFinal size of mylist is " << mylist.size() << '\n';

 

    // list::push_back

    int myint;

    cout << "Please enter some integers (enter 0 to end):\n";

    do {

        cin >> myint;

        mylist.push_back (myint);

    } while (myint);

    cout << "mylist contains:";

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    // list::pop_back

    while (!mylist.empty()) {

        cout << ' ' <<  mylist.back();

        mylist.pop_back();

    }

    cout << "\nFinal size of mylist is " << mylist.size() << '\n';

 

    return 0;

}

输出结果:

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):

23 8 5 6 0

mylist contains: 56 23 8 5 6 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);  //firstlast是我们选择的把值插入到这个list中的值所在的容器的迭代器

// inserting into a list

#include <iostream>

#include <list>

#include <vector>

using namespace std;

int main () {

    list<int> mylist;

    list<int>::iterator it;

    // 初始化

    for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5

    it = mylist.begin();

    ++it;       // 迭代器it现在指向数字2                      ^

    //i0t指向的位置出插入元素10

    mylist.insert (it,10);                        // 1 10 2 3 4 5

 

    // "it" 仍然指向数字2                                   ^

    //it指向的位置出插入两个元素20

    mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5

 

    --it;       // 现在it指向数字20                             ^

 

    vector<int> myvector (2,30); //创建vector容器,并初始化为含有2个值为30的元素

    //vector容器的值插入list

    mylist.insert (it,myvector.begin(),myvector.end());

                                                // 1 10 20 30 30 20 2 3 4 5

    //it仍然指向数字20                            //               ^

    cout << "mylist contains:";

    for (it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    return 0;

}

输出结果:

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)中的值,也可以不用变量接收其返回值

// erasing from list

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist;

    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//       ^

    cout << "*it1 : " << *it1 << endl;

    cout << "mylist contains:";

    for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)

        cout << ' ' << *it1;

    cout << '\n';

  return 0;

}

输出结果:

it1 : 40

mylist contains: 10 30 60 80 90

swap  //交换两个list的内容

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

// swap lists

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> first (3,100);   // 三个值为100的元素

    list<int> second (5,200);  // 五个值为200的元素

 

    first.swap(second);

 

    cout << "first contains:";

    for (list<int>::iterator it = first.begin(); it != first.end(); it++)

        cout << ' ' << *it;

    cout << '\n';

 

    cout << "second contains:";

    for (list<int>::iterator it = second.begin(); it != second.end(); it++)

        cout << ' ' << *it;

    cout << '\n';

 

    return 0;

}

输出结果:

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;

// resizing list

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist;

 

    // 初始化

    for (int i = 1; i < 10; ++i) mylist.push_back(i);

 

    mylist.resize(5);

    mylist.resize(8,100);

    mylist.resize(12);

 

    cout << "mylist contains:";

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

mylist contains: 1 2 3 4 5 100 100 100 0 0 0 0

clear  //清空list

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

// clearing lists

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist;

    list<int>::iterator it;

 

    mylist.push_back (100);

    mylist.push_back (200);

    mylist.push_back (300);

 

    cout << "mylist contains:";

    for (it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    mylist.clear();

    mylist.push_back (1101);

    mylist.push_back (2202);

 

    cout << "mylist contains:";

    for (it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

mylist contains: 100 200 300

mylist contains: 1101 2202

6Operations:

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

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

void splice (iterator position, list& x, iterator i);  //将列表x中迭代器i 指向的元素移到当前listposition指向的位置处,由于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的迭代器

// splicing lists

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist1, mylist2;

    list<int>::iterator it;

 

    // 初始化

    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;                         // 指向数字2

 

    mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4

                                  // mylist2 (empty)

                                  // "it" 仍然指向数字2

 

    mylist2.splice (mylist2.begin(),mylist1, it);

                                // mylist1: 1 10 20 30 3 4

                                // mylist2: 2

                                // "it" 此时已经无效了

    it = mylist1.begin();

    advance(it,3);           // "it" 指向数字30

 

    mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());

                                // mylist1: 30 3 4 1 10 20

 

    cout << "mylist1 contains:";

    for (it = mylist1.begin(); it != mylist1.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    cout << "mylist2 contains:";

    for (it = mylist2.begin(); it != mylist2.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

mylist1 contains: 30 3 4 1 10 20

mylist2 contains: 2

remove  //删除list中特定的值

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

// remove from list

#include <iostream>

#include <list>

using namespace std;

int main () {

    int myints[]= {17, 89, 7, 89, 14};

    list<int> mylist (myints,myints+5);

 

    mylist.remove(89);

 

    cout << "mylist contains:";

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    return 0;

}

输出结果:

mylist contains: 17 7 14

remove_if  //按条件删除

template <class Predicate>

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

// list::remove_if

#include <iostream>

#include <list>

using namespace std;

// 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};

    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

 

    cout << "mylist contains:";

    for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    return 0;

}

输出结果:

mylist contains: 36 20

unique  //删除重复值

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

template <class BinaryPredicate>

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

// list::unique

#include <iostream>

#include <cmath>

#include <list>

using namespace std;

// 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 };

    list<double> mylist (mydoubles,mydoubles+10);

    cout << "mylist contains:";

    for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

    

    mylist.unique();

    cout << "mylist contains:";

    for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    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

    cout << "mylist contains:";

    for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15

                                       // 15.3,  72.25, 73.0

    cout << "mylist contains:";

    for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    mylist.unique (is_near());           //  2.72, 12.15, 72.25

 

    cout << "mylist contains:";

    for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

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中存储的元素类型相同,当满足条件时返回truemerge就按照这个条件将两个列表合并

// list::merge

#include <iostream>

#include <list>

using namespace std;

// compare only integral part:

bool mycomparison (double first, double second) { return ( (first)<(second) ); }

 

int main () {

    list<double> first, second;

 

    first.push_back (3.1);

    first.push_back (2.2);

    first.push_back (2.9);

 

    second.push_back (3.7);

    second.push_back (7.1);

    second.push_back (1.4);

 

    first.sort();

    second.sort();

 

    first.merge(second);

    cout << "first contains:";

    for (list<double>::iterator it = first.begin(); it != first.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

    // (second 现在为空)

 

    second.push_back (2.1);

    second.push_back(2.5);

 

    first.merge(second,mycomparison);

    cout << "first contains:";

    for (list<double>::iterator it = first.begin(); it != first.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

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中元素类型相同,满足条件时返回truesort()函数就按照comp中制定的规则对元素进行排序

// list::sort

#include <iostream>

#include <list>

#include <string>

#include <cctype>

using namespace std;

// comparison, not case sensitive.

bool compare_nocase (const string& first, const 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 () {

    list<string> mylist;

    list<string>::iterator it;

    mylist.push_back ("one");

    mylist.push_back ("two");

    mylist.push_back ("Three");

 

    mylist.sort();

 

    cout << "mylist contains:";

    for (it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

    mylist.sort(compare_nocase);

 

    cout << "mylist contains:";

    for (it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

 

  return 0;

}

输出结果:

mylist contains: Three one two

mylist contains: one Three two

reverse  //逆序:

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

// reversing list

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist;

    for (int i = 1; i < 10; ++i) mylist.push_back(i);

    mylist.reverse();

    cout << "mylist contains:";

    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)

        cout << ' ' << *it;

    cout << '\n';

  return 0;

}

输出结果:

mylist contains: 9 8 7 6 5 4 3 2 1

7Observers:

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

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

// list::get_allocator

#include <iostream>

#include <list>

using namespace std;

int main () {

    list<int> mylist;

    int * p;

 

    // allocate an array of 5 elements using mylist's allocator:

    p = mylist.get_allocator().allocate(5);

 

    // assign some values to array

    for (int i = 0; i < 5; ++i) p[i] = i;

 

    cout << "The allocated array contains:";

    for (int i = 0; i < 5; ++i) cout << ' ' << p[i];

    cout << '\n';

 

    mylist.get_allocator().deallocate(p,5);

 

    return 0;

}

输出结果:

The allocated array contains: 9 8 7 6 5 4 3 2 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值