设为首页 加入收藏

TOP

标准模板库(STL)List介绍(十三)
2011-06-07 12:31:23 来源: 作者: 【 】 浏览:9191
Tags:标准 模板 STL List 介绍
 
  
用list的成员函数插入元素到list中  
list的成员函数push_front()和push_back()分别把元素加入到list的前面和后面。你可以使用insert() 把对象插入到list中的任何地方。  
insert()可以加入一个对象,一个对象的若干份拷贝,或者一个范围以内的对象。这里是一些 插入对象到list中的例子:  
  
/*  
|| Using insert to insert elements into a list.  
*/  
#include <list>  
   
int main (void) {  
     list<int> list1;  
   
     /*  
     || Put integers 0 to 9 in the list  
     */  
     for (int i = 0; i < 10; ++i) list1.push_back(i);  
   
     /*  
     || Insert -1 using the insert member function  
     || Our list will contain -1,0,1,2,3,4,5,6,7,8,9  
     */  
         list1.insert(list1.begin(), -1);  
   
     /*  
     || Insert an element at the end using insert  
     || Our list will contain -1,0,1,2,3,4,5,6,7,8,9,10  
     */  
         list1.insert(list1.end(), 10);  
   
     /*  
     || Inserting a range from another container  
     || Our list will contain -1,0,1,2,3,4,5,6,7,8,9,10,11,12  
     */  
     int IntArray[2] = {11,12};  
     list1.insert(list1.end(), &IntArray[0], &IntArray[2]);  
   
     /*  
     || As an exercise put the code in here to print the lists!  
     || Hint: use PrintIt and accept an interger  
     */  
}  
  
注意,insert()函数把一个或若干个元素插入到你指出的iterator的位置。你的元素将出现在 iterator指出的位置以前。  
  
  
首页 上一页 10 11 12 13 14 15 16 下一页 尾页 13/18/18
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 C++语言的url encode 和decode 下一篇C++ map的基本操作和使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: