最全STL之list介绍_stl list,2024年最新已有千人收藏

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对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++)
    {


![img](https://img-blog.csdnimg.cn/img_convert/0f84bbab181a31d58603fadf057ead56.png)
![img](https://img-blog.csdnimg.cn/img_convert/d60e7d6e05b77921951feb95686af491.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

25)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值