STL标准库中的list接口

                                     list接口
在标准STL库中list接口如下:

list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。

使用list容器之前必须加上<vector>头文件:#include<list>;

list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;

1.构造函数、拷贝构造函数
int main()
{
list<int> l1; //空链表
list<int> l2(3); //建一个含三个默认值是的元素的链表
list<int> l3(5,2); //建一个含五个元素的链表,值都是
list<int> l4(l3); //建一个的copy链表
list<int> l5(++l3.begin(),--l3.end()); c5含c1一个区域的元素[_First, _Last)。
list<int>::iterator it1;//使用迭代器去遍历l1对象里的元素
list<int>::iterator it2;//使用迭代器去遍历l2对象里的元素
list<int>::iterator it3;//使用迭代器去遍历l3对象里的元素
list<int>::iterator it4;//使用迭代器去遍历l4对象里的元素
list<int>::iterator it5;//使用迭代器去遍历l5对象里的元素
it1 = l1.begin();
while (it1 != l1.end())
{
     cout << *it1 << " ";
     ++it1;
}
cout << endl;
it2 = l2.begin();
while (it2 != l2.end())
{
     cout << *it2 << " ";
     ++it2;
}
cout << endl;
it3 = l3.begin();
while (it3 != l3.end())
{
     cout << *it3 << " ";
     ++it3;
}
cout << endl;
it4 = l4.begin();
while (it4 != l4.end())
{
     cout << *it4 << " ";
     ++it4;
}
cout << endl;
it5 = l5.begin();
while (it5 != l5.end())
{
     cout << *it5 << " ";
     ++it5;
}
cout << endl;
return 0;
}


 2.迭代器成员函数
 begin()      返回指向链表第一个元素的迭代器。
 end()       返回指向链表最后一个元素之后的迭代器。
c.begin()      返回指向链表第一个元素的迭代器。(c++11标准)
c.end()      返回指向链表最后一个元素之后的迭代器。(c++11标准) 
it4 = l4.begin();
while (it4 != l4.end())
{
     *it4 = 1;
     cout << *it4 << " ";
     ++it4;
}
cout << endl;
结果显示:1>f:\程序\stl接口\stl接口\源.cpp(35): error C3892: “it4”: 不能给常量赋值
rbegin()      返回逆向链表的第一个元素,即c链表的最后一个数据。
rend()      返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
void  Test1()
{
     list<int>l1;
     l1.push_back(1);
     l1.push_back(2);
     l1.push_back(3);
     l1.push_back(4);
     list<int>::reverse_iterator it = l1.rbegin();
     while (it != l1.rend())
     {
          cout << *it << " ";
          ++it;
     }
}
结果是:4 3 2 1。
c.rbegin()      返回逆向链表的第一个元素,即c链表的最后一个数据。(c++11)
c.rend()      返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。(c++11)
类似rbegin()和rend()只不过不可赋值。
3.Capacity:关于容量的一些方法
empty();
  bool empty() const
size();
  size_type size() const  
max_size():
 size_type max_size() const
  resize(size_type new_size, const T& x);// 新的大小并将最后一个数赋x
  resize(size_type new_size);//新的大小
void Test2()
{
     list<int>l1(3);
     cout << l1.empty()<<endl;//判断链表空不空 
     cout << l1.size()<<endl;//结点的个数
     cout << l1.max_size()<<endl;  //返回
      l1.resize(5);// 使链表变成个结点
      cout << l1.size() << endl;
       l1.resize(6, 6);//将链表变成个值为的结点
      cout << l1.size() << endl;
      list<int>::iterator it5;
           it5 = l1.begin();
      while (it5 != l1.end())
      {
          cout << *it5 << " ";
          ++it5;
      }
      cout << endl;
}
4.element access
 
 reference front() ;// 返回*begin()
  const_reference front() const ;
  reference back() ; //返回*(--end())
  const_reference back() const  ;
5. push_back    pop_back    push_front   pop_front    
void Test3()
{ 
     // push_back    pop_back
    list<int> l4;
     l4.push_back(1);
     l4.push_back(2);
     l4.push_back(3);
     l4.push_back(4);
     l4.pop_back();
     l4.pop_back();
     l4.pop_back();
     list<int>::iterator it1=l4.begin();
     while (it1!=l4.end())
     {
          cout<<*it1<<" ";
          ++it1;
     }
     cout<<endl;
   //push_front   pop_front
      list<int> l5;
      l5.push_front(5);
       l5.push_front(6);
         l5.push_front(7);
           l5.pop_back();
           l5.pop_back();
list<int>::iterator it2=l5.begin();
     while (it2!=l5.end())
     {
          cout<<*it2<<" ";
          ++it2;
     }
     cout<<endl;
 }
6.insert  erase  clear   swap
iterator insert ( iterator position, const T& x );//在position位置插入元素num。
insert(iterator position, size_type n, const T& x);//  在position位置插入n个元素num
insert ( iterator position, InputIterator first, InputIterator last );//在position位置插入区间为[first,last)的元素
iterator erase(iterator position);//删除position
 iterator erase(iterator first, iterator last);//删除区间为[first,last)的元素
void  Test4()
{
     list<int>l1;
     list<int>l2;
     list<int>l3;
     l1.insert(l1.begin(), 1);
     l2.insert(l2.begin(), 8, 2);
     l3.insert(l3.begin(), ++l2.begin(), --l2.end());
     list<int>::iterator it1;
     it1 =l1.begin();
     while (it1 !=l1.end())
     {
          cout << *it1 << " ";
          ++it1;
     }
     cout << endl;
     list<int>::iterator it2;
     it2 = l2.begin();
     while (it2 != l2.end())
     {
          cout << *it2 << " ";
          ++it2;
     }
     cout << endl;
     list<int>::iterator it3;
     it3= l3.begin();
     while (it3 != l3.end())
     {
          cout << *it3 << " ";
          ++it3;
     }
     cout << endl;

 //erase
 l2.erase(l2.begin());
l3.erase(++l3.begin(), l3.end());
list<int>::iterator it4;
it4= l2.begin();
while (it4 != l2.end())
{
     cout << *it4 << " ";
     ++it4;
}
cout << endl;
list<int>::iterator it5;
it5= l3.begin();
while (it5 != l3.end())
{
     cout << *it5 << " ";
     ++it5;
}
cout << endl;

 //clear
l3.clear();
list<int>::iterator it6;
it6= l3.begin();
while (it6 != l3.end())
{
     cout << *it6 << " ";
     ++it6;
}
cout << endl;
 //swap
     l1.swap(l2);
 
     list<int>::iterator it7;
     it7= l2.begin();
     while (it7 != l2.end())
     {
          cout << *it7 << " ";
          ++it7;
     }
     cout << endl;
     list<int>::iterator it8;
     it8= l1.begin();
     while (it8 != l1.end())
     {
          cout << *it8 << " ";
          ++it8;
     }
     cout << endl;
}

7.splice
void  Test5()
 {
      list<int> l1;
      list<int> l2;
      list<int>  l3(l2);
      l1.insert(l1.begin(),5,8);
      l2.insert(l2.begin(),9,10);
      l1.splice(l1.begin(),l2); //将l2连接在l1的begin位置,释放l2
      l3.splice(l1.begin(),l2,l2.begin(),l2.end());// 将l2的[begin,end)位置的元素连接到l1的begin位置并且释放l2的[begin,end)位置的元素
 list<int>::iterator it1;
      it1 =l1.begin();
      while (it1 !=l1.end())
      {
           cout << *it1 << " ";
           ++it1;
      }
      cout << endl;
      list<int>::iterator it2;
      it2 = l2.begin();
      while (it2 != l2.end())
      {
           cout << *it2 << " ";
           ++it2;
      }
      cout << endl;
      list<int>::iterator it3;
      it3 =l1.begin();
      while (it3 !=l1.end())
      {
           cout << *it3 << " ";
           ++it3;
      }
      cout << endl;     
 }

8.sort和unique
void Test6()
 {
      //sort
    list<int> l1;
     l1.push_back(1);
     l1.push_back(2);
     l1.push_back(5);
     l1.push_back(4);
     l1.push_back(5);
     l1.push_back(4);
     l1.push_back(3);
     l1.sort();
     list<int>::iterator it1;
     it1 =l1.begin();
     while (it1 !=l1.end())
     {
          cout << *it1 << " ";
          ++it1;
     }
     cout << endl;
     //reverse
     l1.reverse();
     list<int>::iterator it2;
     it2 = l1.begin();
     while (it2 != l1.end())
     {
          cout << *it2 << " ";
          ++it2;
     }
     cout << endl;

 //unique
l1.unique();
list<int>::iterator it3;
it3 = l1.begin();
while (it3 != l1.end())
{
     cout << *it3 << " ";
     ++it3;
}
cout << endl;}
9.merge
void Test7()
{
    //void merge(list<T, Allocator>& x);
    list<int>l1(2, 1);
    list<int>l2(2, 2);
    l1.merge(l2);//把l2中所有的元素都合并到l1中
    list<int>::iterator it1 = l1.begin();
    while (it1 != l1.end())
    {
        cout << *it1 << " ";
        it1++;
    }
    cout << endl;}
10.remove
 
void Test8()
 {
    list<int> l1(5,1);
     l1.remove(1);//删除节点为1的数据
     list<int>::iterator it1 = l1.begin();
     while (it1 != l1.end())
     {
          cout << *it1 << " ";
          it1++;
     }
     cout << endl;
 }

11. 运算符重载

operator==

operator!=

operator<

operator<=

operator>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值