C++ STL list

list(链表)

链表也即链式表,在数据结构中,我们知道线性表的物理存储结构有两种,顺序表(数组)和链式表(结点)。链表是在堆中为每一个元素分配内存,然后利用指针将所有元素串起来。根据这种物理存储结构,我们很容易明白:链表在插入、删除效率上比较高,但没有办法随机访问,因此访问速度慢。
顺序表在STL中的实现为vector,其优点是元素访问速度快(元素存储在连续的数组中,可以直接通过索引计算元素地址),但插入、删除效率较低。

头文件:#include
构造函数
// constructors used in the same order as described above:
std::list<int> first;       // empty list of ints
std::list<int> second (4,100);       // four ints with value 100
std::list<int> third (second.begin(),second.end());  // iterating through second
std::list<int> fourth (third);  // a copy of third

// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

std::cout << "The contents of fifth are: ";
for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
std::cout << *it << ' ';

std::cout << '\n';


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
迭代器

list<int>::iterator it;
list<int>::reverse_iterator rit;
it = second.begin();
it = second.end();
rit = second.rbegin();
rit = second.rend();


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
容器大小

cout << second.empty() << endl;
cout << second.size() << endl;
cout << second.max_size() << endl;

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
元素操作

second.push_back(20);
second.pop_back();
second.push_front(30);
second.pop_front();

second.insert(second.begin(), 40);
second.insert(second.begin(), 2, 200);   //插入2个200
second.insert(second.begin(), third.begin(), third.end());

second.erase(second.begin());
second.erase(second.begin(), second.end());

second.swap(third);
second.resize(10);
second.clear();

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

所有的插入操作都是插入到iterator指向的元素之前

其它操作
  • 由于list是链式存储,因此不能随机访问,也即不支持索引访问。
//链表拼接
std::list<int> mylist1, mylist2;
std::list<int>::iterator it;

// set some initial values:
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++;

//将mylist2拼接到mylist1的it指向元素前面
mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)

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



//删除(根据元素值删除,而不是iterator)
mylist2.remove(20);

//去除重复元素,必须是针对已经排序过的,因为这里只是去除连续重复的元素
mylist2.sort();
mylist2.unique();

mylist1.sort();
mylist1.merge(mylist2);   
//merge与splice一样,合并过后,mylist2为空
//同时,合并之前必须对各个list进行排序

mylist1.reverse();  //使得mylist1各个元素逆向

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

文章转载自https://blog.csdn.net/cypress1010/article/details/53669403

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值