STL之list介绍

本文主要介绍STL中的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 的简单代码示例。

示例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,代码(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;
}

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



猜你喜欢

转载自blog.csdn.net/liitdar/article/details/80529447
今日推荐