STL中list用法解析

   STL是StandardTemplateLibrary的简称,标准模板库,惠普实验室开发的一系列软件的统称。它是由AlexanderStepanov、MengLee和DavidRMusser在惠普实验室工作时所开发出来的。这可能是一个历史上最令人兴奋的工具的最无聊的术语。从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其他一些组件的集合。这里的“容器”和算法的集合指的是世界上很多聪明人很多年的杰作。STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。STL现在是C++的一部分,因此不用额外安装什么.

   这里,我将会讲解标准库里的list的使用方法。
 
1构造函数

这里举例几种方法定义一个list.
(1)定义list为空时(不放任何元素)    list<int> first;
(2)定义list中的元素为n相同的值       list<int> second(4,100);
(3)定义list以另外定义好的list的begin()和end()来实现     list<int> third(second.begin(),second.end());
(4)定义list直接以另一个list拷贝构造  list<int> fourth (third);

2 完成opertator=的重载
 

list<int> first(1);//定义一个类型为list<int> 的first,  first.begin()为1
list<int> second(3);//定义一个类型为list<int> 的second,  second.begin()为3
first=second;

3插入元素
(1)用list中的push_back(尾插)和push_front(头插)插入元素到list中
list<int> mylist;
mylist.push_back(1);
mylist.push_back(2);
mylist.push_back(3);
mylist.push_front(0);
(2)用insert插入元素
list<int> first(5,1);//定义一个list存放 5 个1   first: 1,1,1,1,1,
first.insert(first.begin(),999);在begin位置前插入 ,则first: 999,1,1,1,1,1,
first.insert(first.end(),666);在end位置前插入,    first:999,1,1,1,1,1,666


在指定位置插入n个相同值得元素
// inserting into a list
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5

  it = mylist.begin();
  ++it;       // it points now to number 2           ^

  mylist.insert (it,10);                        // 1 10 2 3 4 5

  // "it" still points to number 2                      ^
  mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5

  --it;       // it points now to the second 20            ^
std::cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


4 删除元素
(1)用 pop_front(头删)和pop_back(尾删)
list<int> first;声明一个list
first.push_back(1);
first.push_back(2);
first.push_back(3);//插入1,2,3,到first中。first:1,2,3,
first.pop_front();//删除第一个元素  first:2,3
first.pop_back();//删除最后一个元素  first:2

(2)用erase删除指定位置元素
   
// erasing from list
#include <iostream>
#include <list>

int main ()
{
  std::list<int> mylist;
  std::list<int>::iterator it1,it2;

  // set some values:
  for (int i=1; i<10; ++i) mylist.push_back(i*10);

                              // 10 20 30 40 50 60 70 80 90
  it1 = it2 = mylist.begin(); // ^^
  advance (it2,6);            // ^                 ^
  ++it1;                      //    ^              ^

  it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
                              //    ^           ^

  it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
                              //    ^           ^

  ++it1;                      //       ^        ^
  --it2;                      //       ^     ^

  mylist.erase (it1,it2);     // 10 30 60 80 90
                              //        ^

  std::cout << "mylist contains:";
  for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
    std::cout << ' ' << *it1;
  std::cout << '\n';

  return 0;
}



5遍历list中的元素
#include <iostream>
#include <list>

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::list<int> mylist (myints,myints+5);

  std::cout << "mylist contains:";
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

定义了一个迭代器it,其指向了mylist中的第一个元素,通过mylist.begin()来得到。再与mylist.end()的返回值比较。
 for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)

6 list是否为空,为空则返回真


 while (!mylist.empty())//mylist不空循环
  {
     sum += mylist.front();
     mylist.pop_front();
  }


7正向迭代器 begin  ,end.   反向迭代器  rbegin ,  rend






      STL的关键实际上是iterator。STL算法作为参数使用iterator,他们指出一个范围,有时是一个范围, 有时是两个。STL容器支持iterator,这就是为什么我们说 list<int>::iterator, 或 list<char>::iterator, 或 list<string>::iterator.
     iterator有很好的定义继承性。它们非常有用。某些iterator仅支持对一个容器只读,某些 仅支持写,还有一些仅能向前指,有一些是双向的。有一些iterator支持对一个容器的随机存取。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值