C++ stl list的用法

来源:互联网 发布:温州网络作家协会 编辑:程序博客网 时间:2024/05/19 12:39

#include <iostream> 

#include <list> 

#include <numeric> 

#include <algorithm> 

 

using namespace std; 

 

//创建一个list容器的实例LISTINT 

typedef list<int> LISTINT; 

 

//创建一个list容器的实例LISTCHAR 

typedef list<char> LISTCHAR; 

//从前向后显示list队列的全部元素 

typedef list<int> INTLIST;

 

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; 

}; 

 

class IsAToothbrush 

{

public:

IsAToothbrush(string& InToothbrushCode):ToothbrushCode(InToothbrushCode){}; 

bool operator()(string& SalesRecord) 

{

return (0 == SalesRecord.substr(0,4).compare(ToothbrushCode));

private:

string ToothbrushCode; 

};

 

void main(void) 

    //-------------------------- 

    //用list容器处理整型数据 

    //-------------------------- 

    //用LISTINT创建一个名为listOne的list对象 

    LISTINT listOne; 

    //声明i为迭代器 

    LISTINT::iterator i; 

    

    //从前面向listOne容器中添加数据 

    listOne.push_front (2); 

    listOne.push_front (1); 

    

    //从后面向listOne容器中添加数据 

    listOne.push_back (3); 

    listOne.push_back (4); 

    

    //从前向后显示listOne中的数据 

    cout<<"listOne.begin()--- listOne.end():"<<endl; 

    for (i = listOne.begin(); i != listOne.end(); ++i) 

        cout << *i << " "; 

    cout << endl; 

    

    //从后向后显示listOne中的数据 

    LISTINT::reverse_iterator ir; 

    cout<<"listOne.rbegin()---listOne.rend():"<<endl; 

    for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) { 

        cout << *ir << " "; 

    } 

    cout << endl; 

    

    //使用STL的accumulate(累加)算法 

    int result = accumulate(listOne.begin(), listOne.end(),0); 

    cout<<"Sum="<<result<<endl; 

    cout<<"------------------"<<endl; 

    

    //-------------------------- 

    //用list容器处理字符型数据 

    //-------------------------- 

    

    //用LISTCHAR创建一个名为listOne的list对象 

    LISTCHAR listTwo; 

    //声明i为迭代器 

    LISTCHAR::iterator j; 

    

    //从前面向listTwo容器中添加数据 

    listTwo.push_front ('A'); 

    listTwo.push_front ('B'); 

    

    //从后面向listTwo容器中添加数据 

    listTwo.push_back ('x'); 

    listTwo.push_back ('y'); 

    

    //从前向后显示listTwo中的数据 

    cout<<"listTwo.begin()---listTwo.end():"<<endl; 

    for (j = listTwo.begin(); j != listTwo.end(); ++j) 

        cout << char(*j) << " "; 

    cout << endl; 

    

    //使用STL的max_element算法求listTwo中的最大元素并显示 

    j=max_element(listTwo.begin(),listTwo.end()); 

    cout << "The maximum element in listTwo is: "<<char(*j)<<endl; 

 

//list1对象初始为空 

    INTLIST list1; 

    //list2对象最初有10个值为6的元素 

    INTLIST list2(10,6); 

    //list3对象最初有9个值为6的元素 

    INTLIST list3(list2.begin(),--list2.end()); 

 

//从前向后显示各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序列中间插入数据 

    list1.insert(++list1.begin(),3,9); 

    cout<<"list1.insert(list1.begin()+1,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;//9个6 

 

 

//对list1容器排序 

    list1.sort(); //2 5 9 9

    put_list(list1,"list1"); 

 

//合并处理 

    list1.splice(++list1.begin(), list3); //2 (9个6) 5 9 9  

    put_list(list1,"list1"); 

    put_list(list3,"list3"); // 空

 

//通常把一些错误信息push_back()到一个list中去,然后push_front()一个标题到list中, 

//这样它就会在这个错误消息以前打印它了。

list<string> Milkshakes; //声明了list<string>模板类的一个实例

Milkshakes.push_back("Chocolate");     //实例化

Milkshakes.push_back("Strawberry");

Milkshakes.push_front("Lime");

Milkshakes.push_front("Vanilla");

// print the milkshakes打印

Milkshakes.push_front("The Milkshake Menu");

Milkshakes.push_back("*** Thats the end ***");

list<string>::iterator MilkshakeIterator;

for (MilkshakeIterator=Milkshakes.begin(); MilkshakeIterator!=Milkshakes.end();++MilkshakeIterator) 

{

// dereference the iterator to get the element

cout << (*MilkshakeIterator).c_str() << endl; //输出

   

//用STL的通用算法count_if()来统计list中的元素个数

list<string> SalesRecords;

 

SalesRecords.push_back("0001 Soap");

SalesRecords.push_back("0002 Shampoo");

SalesRecords.push_back("0003 Toothbrush");

SalesRecords.push_back("0004 Toothpaste");

SalesRecords.push_back("0003 Toothbrush");

//error 没有预留内存

//cout<<"SalesRecords.capacity(): "<<SalesRecords.capacity()<<endl; 

cout<<"SalesRecords.size(): "<<SalesRecords.size()<<endl; //6

//第一次找到0003前缀,则删除

list<string>::iterator SalesRecordsIterator;

for (SalesRecordsIterator=SalesRecords.begin(); SalesRecordsIterator!=SalesRecords.end();++SalesRecordsIterator) 

{

// dereference the iterator to get the element

//cout << (*SalesRecordsIterator).c_str() << endl; //输出

if(0==((*SalesRecordsIterator).substr(0,4).compare("0003")))

break;

SalesRecords.erase(SalesRecordsIterator);//.erase(SalesRecords.begin())

    cout<<"SalesRecords.size(): "<<SalesRecords.size()<<endl; //5

 

 

/*

list<string>::iterator SalesRecordsIterator;

for (SalesRecordsIterator=SalesRecords.begin(); SalesRecordsIterator!=SalesRecords.end();++SalesRecordsIterator) 

{

// dereference the iterator to get the element

cout << (*SalesRecordsIterator).c_str() << endl; //输出

*/

    //使用count_if 做统计

string VariableToothbrushCode("0003");

    int NumberOfToothbrushes(0); 

NumberOfToothbrushes = count_if(SalesRecords.begin(),SalesRecords.end(), \

IsAToothbrush(VariableToothbrushCode));

//2

cout << "There were " << NumberOfToothbrushes <<" toothbrushes matching code"<<VariableToothbrushCode.c_str()<<" sold "<<endl;

/*

list就是双向链表 ,元素也是在堆中存放, 每个元素都是放在一块内存中,它的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随机存取变的非常没有效率 ,因此它没有提供[]操作符的重载。但由于链表的特点,它可以以很好的效率支持任意地方的删除和插入。

list没有空间预留(没有capacity方法),所以每分配一个元素都会从内存中分配,每删除一个元素都会释放它占用的内存.

list在哪里添加删除元素性能都很高,不需要移动内存,当然也不需要对每个元素都进行构造与析构了,所以常用来做随机操作容器. 

但是访问list里面的元素时就开始和最后访问最快 

访问其它元素都是O(n) ,所以如果需要经常随机访问的话,还是使用其它的好

总结 

如果经常添加删除大对象的话,那么请使用list 

要保存的对象不大,构造与析构操作不复杂,那么可以使用vector代替 

list<指针>完全是性能最低的做法,这种情况下还是使用vector<指针>好,因为指针没有构造与析构,也不占用很大内存 

List的迭代器更像是指针,不可以算术操作

list

    双向链表

    每一个结点都包括一个信息块Info、一个前驱指针Pre、一个后驱指针Post。可以不分配必须的内存大小方便的进行添加和删除操作。使用的是非连续的内存空间进行存储。

   优点:(1) 不使用连续内存完成动态操作。

            (2) 在内部方便的进行插入和删除操作

            (3) 可在两端进行push、pop

   缺点:(1) 不能进行内部的随机访问,即不支持[ ]操作符和.at()

            (2) 相对于verctor占用内存多

*/

0 0
原创粉丝点击