C++ list容器使用

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

//AUTHOR: lanyang123456

//DATE: 2014-10-21

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





 #include <iostream>

#include <list>

using namespace std;


int main()

{

   list<int> L;

   L.push_back(0);              // Insert a new element at the end

   L.push_front(0);             // Insert a new element at the beginning

   L.insert(++L.begin(),2);     // Insert "2" before position of first argument

                                            // (Place before second argument)

   L.push_back(5);

   L.push_back(6);

   list<int>::iterator i;

   for(i=L.begin(); i != L.end(); ++i) cout << *i << " ";

   cout << endl;

   return 0;

}



$ g++ -o test list.cpp

$ ./test
0 2 0 5 6



再举一个字符串的例子



 #include <iostream>

#include <list>

using namespace std;


int main()

{

   list<string> L;

   L.push_back("Tom");              // Insert a new element at the end

   L.push_front("Jerry");             // Insert a new element at the beginning

   L.insert(++L.begin(), "Mily");     // Insert "2" before position of first argument

                                            // (Place before second argument)

   L.push_back("Alex");

   L.push_back("Jack");

   cout<<"list size = "<<L.size()<<endl;
   cout<<"list max_size = "<<L.max_size()<<endl;

   list<string>::iterator iter;

   for(iter = L.begin(); iter != L.end(); ++iter) 
	cout<<*iter<<endl;

   L.erase(--iter);//remove the last one
   cout<<"------after erase---------"<<endl;
   cout<<"list size = "<<L.size()<<endl;
   cout<<"list max_size = "<<L.max_size()<<endl;

   for(iter = L.begin(); iter != L.end(); ++iter) 
	cout<<*iter<<endl;

   return 0;

}


/*

$ ./test 
list size = 5
list max_size = 768614336404564650
Jerry
Mily
Tom
Alex
Jack
------after erase---------
list size = 4
list max_size = 768614336404564650
Jerry
Mily
Tom
Alex


*/






参考

http://blog.youkuaiyun.com/whz_zb/article/details/6831817

http://blog.chinaunix.net/uid-26527046-id-3465518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值