C++编程语言STL之list介绍

本文主要介绍C++编程语言的STL(Standard Template Library)中list的相关知识,同时通过示例代码介绍list的常见用法。

1 概述

引用list的C++官方描述,如下:

Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it.

They are very similar to forward_list: The main difference being that forward_list objects are single-linked lists, and thus they can only be iterated forwards, in exchange for being somewhat smaller and more efficient.

Compared to other base standard sequence containers (array, vector and deque), lists perform generally better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists and forward_lists compared to these other sequence containers is that they lack direct access to the elements by their position; For example, to access the sixth element in a list, one has to iterate from a known position (like the beginning or the end) to that position, which takes linear time in the distance between these. They also consume some extra memory to keep the linking information associated to each element (which may be an important factor for large lists of small-sized elements).

2 常见用法

下面介绍两个关于list的示例代码。

2.1 示例1

示例1的代码(list_test.cpp)如下:

// An example using STL list. 
// This program pushes 4 integer values to an STL list.
// It then prints out each of these 4 values before
// deleting them from the list

#include <list>
#include <iostream>

using namespace std;

int main()
{
    typedef list<int> list_t;
    list_t lst;

    int value1 = 10;
    int value2 = 8;

    // Add some values at the end of the list
    lst.push_back(value1);
    lst.push_back(value2);
    lst.push_back(-1);
    lst.push_back(5);
    
    cout << "List values: " << endl;

    // Loop as long as there are still elements in the list.
    while (lst.size() > 0)
    {
    	cout << lst.front() << endl;

    	// Remove the item from the front of the list
    	lst.pop_front();
    }
    
    return 0;
}

编译并执行上述代码,结果如下:

2.2 示例2

示例2的代码(list_test2.cpp)如下:

// Create a random integer list and sort the list

#include <iostream>
#include <list>
#include <stdlib.h>

using namespace std;

int main()
{
    typedef list<int> list_t;
    list_t lst;
    int i;

    // create a list of random integers
    for(i = 0; i < 10; i++)
    {
        lst.push_back(rand());
    }
    
    cout << "Original list: " << endl;
    
    // create an iterator object and make it point to the
    // beginning of the list
    list_t::iterator p = lst.begin();
    while(p != lst.end())
    {
        cout << *p << " ";
        p++;
    }

    cout << endl;
    
    // sort the list
    lst.sort();

    cout <<"Sorted contents:\n";
    p = lst.begin();
    while(p != lst.end())
    {
        cout << *p << " " ;
        p++;
    }

    cout << endl;
    
    return 0;
}

编译并执行上述代码,结果如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值