STL之list

list中的函数代码事例如下:

#include <iostream>
#include <list>

using namespace std;
typedef list<int> INTLIST;

//从前向后显示list队列的全部元素
void put_list(INTLIST list, char *name)
{
    INTLIST::iterator plist;

    cout << "The contents of " << name << " : ";
    for(plist = list.begin(); plist != list.end(); plist++)
        cout << *plist << " ";
    cout<<endl;
}

//测试list容器的功能
int main(){
//list1对象初始为空
    INTLIST list1;   
    //list2对象最初有10个值为6的元素 
    INTLIST list2(10,6); 
    //list3对象最初有9个值为6的元素 
    INTLIST list3(list2.begin(),--list2.end()); 

    //声明一个名为i的双向迭代器
    INTLIST::iterator i;

    //从前向后显示各list对象的元素
    put_list(list1,"list1");
    put_list(list2,"list2");
    put_list(list3,"list3");
    
//从list1序列后面添加两个元素
list1.push_back(2);
list1.push_back(4);
cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
    put_list(list1,"list1");

//从list1序列前面添加两个元素
list1.push_front(5);
list1.push_front(7);
cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
    put_list(list1,"list1");

//在list1序列中间插入数据3个9 
list1.insert(++list1.begin(),3,9);
cout<<"list1.insert(++list1.begin(),3,9):"<<endl;
    put_list(list1,"list1");

//测试引用类函数
cout<<"list1.front()="<<list1.front()<<endl;
cout<<"list1.back()="<<list1.back()<<endl;

//从list1序列的前后各移去一个元素
list1.pop_front();
list1.pop_back();
cout<<"list1.pop_front() and list1.pop_back():"<<endl;
    put_list(list1,"list1");

//清除list1中的第2个元素
list1.erase(++list1.begin());
cout<<"list1.erase(++list1.begin()):"<<endl;
    put_list(list1,"list1");

//对list2赋值并显示
list2.assign(8,1);
cout<<"list2.assign(8,1):"<<endl;
    put_list(list2,"list2");

//显示序列的状态信息
cout<<"list1.max_size(): "<<list1.max_size()<<endl;
cout<<"list1.size(): "<<list1.size()<<endl;
cout<<"list1.empty(): "<<list1.empty()<<endl;

//list序列容器的运算
    put_list(list1,"list1");
    put_list(list3,"list3");
cout<<"list1>list3: "<<(list1>list3)<<endl;
cout<<"list1<list3: "<<(list1<list3)<<endl;

//对list1容器排序
list1.sort();
cout<<"list1.sort(): "<<endl;
    put_list(list1,"list1");
    
//结合处理
list1.splice(++list1.begin(), list3);
cout<<"list1.splice(++list1.begin(), list3)"<<endl;
    put_list(list1,"list1");
    put_list(list3,"list3"); 
cout<<"list3.max_size(): "<<list3.max_size()<<endl;
cout<<"list3.size(): "<<list3.size()<<endl;
cout<<"list3.empty(): "<<list3.empty()<<endl;
}
运行结果:
The contents of list1 : 
The contents of list2 : 6 6 6 6 6 6 6 6 6 6 
The contents of list3 : 6 6 6 6 6 6 6 6 6 
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4 
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4 
list1.insert(++list1.begin(),3,9):
The contents of list1 : 7 9 9 9 5 2 4 
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2 
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2 
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1 
list1.max_size(): 768614336404564650
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2 
The contents of list3 : 6 6 6 6 6 6 6 6 6 
list1>list3: 1
list1<list3: 0
list1.sort(): 
The contents of list1 : 2 5 9 9 
list1.splice(++list1.begin(), list3)
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9 
The contents of list3 : 
list3.max_size(): 768614336404564650
list3.size(): 0
list3.empty(): 1


关于splice(),其解释为:

The splice() function inserts lst at location pos. If specified, the element(s) at del or from start to end are removed.

splice() simply moves elements from one list to another, and doesn't actually do any copying or deleting. Because of this, splice() runs in constant time.

所以splice()实际上相当于将一个链表的指针直接指向了后一个,并删除了后一个的指针,因此其消耗的时间为constant time。所以操作过后作为splice()参数的list被操作部分的元素就没有了。

看个例子:

#include <iostream>
#include <list>
 
std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
{
    for (auto &i : list) {
        ostr << " " << i;
    }
    return ostr;
}
 
int main ()
{
    std::list<int> list1 = { 1, 2, 3, 4, 5 };
    std::list<int> list2 = { 10, 20, 30, 40, 50 };
 
    auto it = list1.begin();
    std::advance(it, 2);
 
    list1.splice(it, list2);
 
    std::cout << "list1: " << list1 << "\n";
    std::cout << "list2: " << list2 << "\n";
 
    list2.splice(list2.begin(), list1, it, list1.end());
 
    std::cout << "list1: " << list1 << "\n";
    std::cout << "list2: " << list2 << "\n";
}
结果为:

list1:  1 2 10 20 30 40 50 3 4 5
list2: 
list1:  1 2 10 20 30 40 50
list2:  3 4 5

其中it代表的位置始终是3的位置,当3的位置变的时候,it也跟着变了,而不是简单的常数,所以我们可以看到iterator跟指针的用法是类似的,可以理解为指针。

这段代码还用到了函数advance(),其用法为:

template< class InputIt, class Distance >
void advance( InputIt& it, Distance n );


it-iterator to be advanced
n-number of elements it should be advanced
例如:

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
 
    auto vi = v.begin();
 
    std::advance(vi, 2);
 
    std::cout << *vi << '\n';
}
结果为:4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值