C++学习之STL(standard Template Libary)::list

来源:互联网 发布:数据分析师报考费用 编辑:程序博客网 时间:2024/06/06 06:35

list的本质是双向循环链表


构造方法有五种

#include <iostream>#include <list>int main (){  // 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';  return 0;}


迭代器


顺序迭代器

begin()  

end()

逆序迭代器

rbegin()

rend()

    for(std::list<int>::iterator iterat = mylist.begin();iterat != mylist.end(); ++iterat){        printf("%#p %d\n",iterat,*iterat);    }
list类对运算符进行了重载

++   等价于  getNext();

--     等价于  getPre();


list是一个环形链表list.begin() == ++ list.end();奇数头插,偶数尾差begin 0X0028FEDC : end 0X0028FEDC end++: 0X0028FEDC0X006004C8 30X00601628 10X006015F8 00X006004B0 20X006004E0 4begin 0X006004C8 : end 0X0028FEDC end++: 0X006004C8偶数头插,奇数尾插begin 0X0028FEDC : end 0X0028FEDC end++: 0X0028FEDC0X005E04E0 40X005E04B0 20X005E15F8 00X005E1628 10X005E04C8 3begin 0X005E04E0 : end 0X0028FEDC end++: 0X005E04E0头插法 begin在不断变化egin 0X0028FEDC : end 0X0028FEDC end++: 0X0028FEDCX007604E0 4X007604C8 3X007604B0 2X00761628 1X007615F8 0egin 0X007604E0 : end 0X0028FEDC end++: 0X007604E0尾插法begin 0X0028FEDC : end 0X0028FEDC end++: 0X0028FEDC0X003E15F8 00X003E1628 10X003E04B0 20X003E04C8 30X003E04E0 4begin 0X003E15F8 : end 0X0028FEDC end++: 0X003E15F8可以看出:集合初始化指向一个对象,对象是一个无数据的值并且这个是一个双向循环链表begin指针记录着第一个结点的指针begin->pre = p;p->next = begin;begin = p;end->next = begin;endend->pre->next = p;p->pre = end->pre;p->next=end;end->pre = p;


容器的状态和大小


max_size()

// list::max_size#include <iostream>#include <list>int main (){  unsigned int i;  std::list<int> mylist;  std::cout << "Enter number of elements: ";  std::cin >> i;  if (i<mylist.max_size()) mylist.resize(i);  else std::cout << "That size exceeds the limit.\n";  return 0;}

在这个地方,成员函数 max_size()通常用来判断resize()申请的空间是否超过容器的最大容量

Here, member max_size is used to check beforehand whether the requested size will be allowed by member resize.

bool  empty()   const noexcept;

// list::empty#include <iostream>#include <list>int main (){  std::list<int> mylist;  int sum (0);  for (int i=1;i<=10;++i) mylist.push_back(i);  while (!mylist.empty())  {     sum += mylist.front();     mylist.pop_front();  }  std::cout << "total: " << sum << '\n';    return 0;}

Test whether container is empty
Returns whether the list container is empty (i.e. whether its size is 0).

This function does not modify the container in any way. To clear the content of a list container, see list::clear.

判断容器是否为空,返回值为  list是否为空 (i.e.  size是否为0 )

此方法不修改容器


size_type size

返回值为当前容器的大小


增加:  push_back(),push_front();insert();swap()

删除:  remove(value),erase(iterator),clear()

查看:  pop_front,pop_back,迭代器

修改:

帮助文档

原创粉丝点击