关于list的一个例子
来源:互联网 发布:apache 监控 插件 编辑:程序博客网 时间:2024/04/24 21:37
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
//assign() use the assign() fill the list
int ary[]={1,2,3,4,5};
list<int> ltest;
ltest.assign(3,100);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout,"--"));
cout << endl;
cout<<endl;
ltest.assign(ary,ary+5);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout,"--"));
cout << endl;
//use the size() and max_size()
cout<<"the list size is: "<<ltest.size()<<" the max_size is: "<<ltest.max_size()<<endl;
cout <<endl;
//we can use the resize() change the list size
list<int> l(10);
cout<<"the size is: "<<l.size()<<endl;
l.resize(100);
cout<<"the change size is: "<<l.size()<<endl;
cout<<endl;
//back() braek the last element
cout<<"the last element is: "<<ltest.back()<<endl;
cout<<endl;
//front() break the first element
cout<<"the first element is: "<<ltest.front()<<endl;
cout<<endl;
//begin() break the first iterator be careful is a iterator,not is emelemt
//Define an iterator
list<int>::iterator It=ltest.begin();
cout<<"the first element is: "<<(*It)<<endl;
cout<<endl;
//end() break the end iterator
while(It!=ltest.end())
cout<<"the list is :"<<(*(It++))<<endl;
cout<<endl;
//use the clear() and empty()
int i;
for(i=0;i<2;++i)
{
if(i==1)
ltest.clear();
if(ltest.empty())
cout<<"the list is null!"<<endl;
else
cout<<"clear success!"<<endl;
}
cout<<endl;
ltest.assign(ary,ary+5);
//pop_back() and pop_front() is delete the last and the first element
ltest.pop_back();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.pop_front();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//push_back() and push_front() is insert a element in the last and first
ltest.push_back(6);
ltest.push_front(10);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//insert() can insert element in the list
//insert(p,t)
//the first usage is insert a t befroe the iterator
ltest.insert(ltest.begin(),8);
cout<<"ltest.insert(ltest.begin(),8): ";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
//insert(p,n,t)
//the second usage is insert some element t before p
ltest.insert(ltest.end(),3,8);
cout<<"ltest.insert(ltest.end(),3,8): ";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
//insert(p,b,e)
//if I bulit a new array ,it's have some value ,I want take a part value insert the list
//we can user the insert(p,b,e),b is the begin,e is the end
int a[]={11,12,13,14,15,16,17};
ltest.insert(ltest.end(),a+2,a+5);
cout<<"ltest.insert(ltest.end(),a+2,a+5) :";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.assign(ary,ary+5);
//rbegin() can return a reverse iterator pointer to the first element
//rend() can return a reverser iterator pointer to the last element
list<int>::reverse_iterator Is = ltest.rbegin();
while(Is!=ltest.rend())
{
cout<<*(Is++)<<" ";
}
cout<<endl;
cout<<endl;
//now we use the reverse() change reverse the element
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.reverse();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
int arr2[]={3,5,4,1,2};
ltest.assign(arr2,arr2+5);
//erase() can delete a element in the list,but we must know the iterator
It=find(ltest.begin(),ltest.end(),5);//can find the 5 int the list
ltest.erase(It);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.erase(ltest.begin());
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//remove can direct the element if it in the list
//if the value not in the list ,list unchange
ltest.assign(ary,ary+5);
ltest.remove(5);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
return 0;
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
//assign() use the assign() fill the list
int ary[]={1,2,3,4,5};
list<int> ltest;
ltest.assign(3,100);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout,"--"));
cout << endl;
cout<<endl;
ltest.assign(ary,ary+5);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout,"--"));
cout << endl;
//use the size() and max_size()
cout<<"the list size is: "<<ltest.size()<<" the max_size is: "<<ltest.max_size()<<endl;
cout <<endl;
//we can use the resize() change the list size
list<int> l(10);
cout<<"the size is: "<<l.size()<<endl;
l.resize(100);
cout<<"the change size is: "<<l.size()<<endl;
cout<<endl;
//back() braek the last element
cout<<"the last element is: "<<ltest.back()<<endl;
cout<<endl;
//front() break the first element
cout<<"the first element is: "<<ltest.front()<<endl;
cout<<endl;
//begin() break the first iterator be careful is a iterator,not is emelemt
//Define an iterator
list<int>::iterator It=ltest.begin();
cout<<"the first element is: "<<(*It)<<endl;
cout<<endl;
//end() break the end iterator
while(It!=ltest.end())
cout<<"the list is :"<<(*(It++))<<endl;
cout<<endl;
//use the clear() and empty()
int i;
for(i=0;i<2;++i)
{
if(i==1)
ltest.clear();
if(ltest.empty())
cout<<"the list is null!"<<endl;
else
cout<<"clear success!"<<endl;
}
cout<<endl;
ltest.assign(ary,ary+5);
//pop_back() and pop_front() is delete the last and the first element
ltest.pop_back();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.pop_front();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//push_back() and push_front() is insert a element in the last and first
ltest.push_back(6);
ltest.push_front(10);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//insert() can insert element in the list
//insert(p,t)
//the first usage is insert a t befroe the iterator
ltest.insert(ltest.begin(),8);
cout<<"ltest.insert(ltest.begin(),8): ";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
//insert(p,n,t)
//the second usage is insert some element t before p
ltest.insert(ltest.end(),3,8);
cout<<"ltest.insert(ltest.end(),3,8): ";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
//insert(p,b,e)
//if I bulit a new array ,it's have some value ,I want take a part value insert the list
//we can user the insert(p,b,e),b is the begin,e is the end
int a[]={11,12,13,14,15,16,17};
ltest.insert(ltest.end(),a+2,a+5);
cout<<"ltest.insert(ltest.end(),a+2,a+5) :";
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.assign(ary,ary+5);
//rbegin() can return a reverse iterator pointer to the first element
//rend() can return a reverser iterator pointer to the last element
list<int>::reverse_iterator Is = ltest.rbegin();
while(Is!=ltest.rend())
{
cout<<*(Is++)<<" ";
}
cout<<endl;
cout<<endl;
//now we use the reverse() change reverse the element
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.reverse();
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
int arr2[]={3,5,4,1,2};
ltest.assign(arr2,arr2+5);
//erase() can delete a element in the list,but we must know the iterator
It=find(ltest.begin(),ltest.end(),5);//can find the 5 int the list
ltest.erase(It);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
ltest.erase(ltest.begin());
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
cout<<endl;
//remove can direct the element if it in the list
//if the value not in the list ,list unchange
ltest.assign(ary,ary+5);
ltest.remove(5);
copy(ltest.begin(),ltest.end(), ostream_iterator<int>(cout," "));
cout<<endl;
return 0;
}
执行结果:
[tgomc@ems03 test]$ ./list
100--100--100--
1--2--3--4--5--
the list size is: 5 the max_size is: 4294967295
the size is: 10
the change size is: 100
the last element is: 5
the first element is: 1
the first element is: 1
the list is :1
the list is :2
the list is :3
the list is :4
the list is :5
clear success!
the list is null!
1 2 3 4
2 3 4
10 2 3 4 6
ltest.insert(ltest.begin(),8): 8 10 2 3 4 6
ltest.insert(ltest.end(),3,8): 8 10 2 3 4 6 8 8 8
ltest.insert(ltest.end(),a+2,a+5) :8 10 2 3 4 6 8 8 8 13 14 15
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
3 4 1 2
4 1 2
1 2 3 4
- 关于list的一个例子
- 关于List 的小例子
- 一个关于Timer()的例子..
- 一个关于异常的例子
- 关于多线程的一个例子
- 关于TabHost的一个例子
- 关于Berkeley的一个例子
- 关于allocator的一个例子
- 关于ListActivity的一个例子
- 关于CSS的一个例子
- 关于abstract的一个例子
- 关于数组的一个例子
- 一个关于this的例子
- 关于setInterval的一个例子
- 关于递归的一个例子
- 关于一个小例子中的list()和explode()函数
- 关于Map和list写的比较烦的一个例子(希望以后可以简化一些)
- 关于java里面Collection包括List,Map等初始化内部匿名类和泛型的一个例子
- 经典的三层设计
- 外业记录:在Windows Azure应用程序中使用基于证书的加密
- 犀利的 oracle 注入技术
- 送给程序员的一句话
- "#"在英语中的意义
- 关于list的一个例子
- 贪多嚼不烂
- 第三章 数据类型、运算符与表达式 (答案)
- VC++多线程编程
- JQuery表格插件之Datatables
- 提取Launcher中的WorkSapce,可以左右滑动切换屏幕页面的类
- 让我弄了一晚上!Oracle遇到了这ora-01033问题
- flex AdvancedDataGrid 高级表格 介绍 教程
- linux上ln命令详细说明