注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

18-3 在list 中插入各种元素的方法!  

2014-04-13 23:55:29|  分类: STL 基本的用法 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

/*和以前的差不的 啊!*/
#include<iostream>
#include<list>
using namespace std;

template<typename T>
void display(const T& input)
{
for(T::const_iterator ielement=input.begin();ielement!=input.end();ielement++)
{
cout<<*ielement<<' '<<endl;
}
cout<<endl;
}
int main()
{
list<int> i;
i.insert (i.begin (),2);
i.insert(i.begin(),1);
//inssert at the end
i.insert (i.end(),3);
cout<<"cout the 1 list:";
display(i);

list<int> i2;
//insert 4 element of the same value 0......
i2.insert (i2.begin(),4,0);
cout<<"cout the 2 list:";
cout<<i2.size ()<<" element of the value:"<<endl;
display(i2);

//insert element from another list at the beginning.....
list<int> i3;
i3.insert (i3.begin (),i.begin (),i.end ());
cout<<"cout the 3 list:";
cout<<i3.size ()<<" element of the value:"<<endl;
display(i3);

//insert element from another list at the ending.....
i3.insert (i3.end (),i2.begin (),i2.end ());
cout<<"cout the 4 list:";
cout<<i3.size ()<<" element of the value:"<<endl;
display(i3);
return 0;
}
/*2014.3.2 22:10
cout the 1 list:1
2
3

cout the 2 list:4 element of the value:
0
0
0
0

cout the 3 list:3 element of the value:
1
2
3

cout the 4 list:7 element of the value:
1
2
3
0
0
0
0

Press any key to continue */


#include <list>
#include <iostream>

using namespace std;

template <typename T>
void DisplayContents (const T& Input)
{
for(auto iElement = Input.cbegin() // auto, cbegin and cend: c++11
; iElement != Input.cend()
; ++ iElement )
cout << *iElement << ' ';

cout << endl;
}

int main ()
{
list <int> listIntegers1;

// Inserting elements at the beginning...
listIntegers1.insert (listIntegers1.begin (), 2);
listIntegers1.insert (listIntegers1.begin (), 1);

// Inserting an element at the end...
listIntegers1.insert (listIntegers1.end (), 3);

cout << "The contents of list 1 after inserting elements:" << endl;
DisplayContents (listIntegers1);

list <int> listIntegers2;

// Inserting 4 elements of the same value 0...
listIntegers2.insert (listIntegers2.begin (), 4, 0);

cout << "The contents of list 2 after inserting '";
cout << listIntegers2.size () << "' elements of a value:" << endl;
DisplayContents (listIntegers2);

list <int> listIntegers3;

// Inserting elements from another list at the beginning...
listIntegers3.insert (listIntegers3.begin (),
listIntegers1.begin (), listIntegers1.end ());

cout << "The contents of list 3 after inserting the contents of ";
cout << "list 1 at the beginning:" << endl;
DisplayContents (listIntegers3);

// Inserting elements from another list at the end...
listIntegers3.insert (listIntegers3.end (),
listIntegers2.begin (), listIntegers2.end ());

cout << "The contents of list 3 after inserting ";
cout << "the contents of list 2 at the beginning:" << endl;
DisplayContents (listIntegers3);

return 0;
}
The contents of list 1 after inserting elements:
1 2 3
The contents of list 2 after inserting '4' elements of a value:
0 0 0 0
The contents of list 3 after inserting the contents of list 1 at the beginning:
1 2 3
The contents of list 3 after inserting the contents of list 2 at the beginning:
1 2 3 0 0 0 0

--------------------------------
Process exited with return value 0
Press any key to continue . . .


  评论这张
 
阅读(7)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017