list .insert() & rbegin()

#include <iostream>
#include <stdlib.h>
#include <list>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
  list<int> mylist;
  list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; i++) mylist.push_back(i); // 1 2 3 4 5

  it = mylist.begin();
  ++it;       // it points now to number 2           ^

  mylist.insert (it,10);                        // 1 10 2 3 4 5

  // "it" still points to number 2                      ^
  mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5

  --it;       // it points now to the second 20            ^

  vector<int> myvector (2,30);
  mylist.insert (it,myvector.begin(),myvector.end());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
  cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); it++)
  cout << " " << *it;
  cout << endl;

   list<int> mlist;
   mlist.insert (mlist.begin(),mylist.rbegin(),mylist.rend());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
  cout << "mylist contains:";
  for (it=mlist.begin(); it!=mlist.end(); it++)
  cout << " " << *it;
  cout << endl;

  system("PAUSE"); 
  return 0;
}

改代码在DEV-C++中科顺利通过,但是在VC中编译时会出现errorC2664的错误,原因在于mlist.insert (mlist.begin(),mylist.rbegin(),mylist.rend());这一行,VC++中的rbegin与标准C++的还是有区别的,当然利用VC的反向iterator或者在标准范围内亦可解决问题。

 

 

 

 

 

 

 

 

 

 

 

 

code learning

默认分类    2008-08-28 22:19   阅读5   评论0  
字号:    

#include <iostream>
#include <stdlib.h>
#include <list>  
#include <iterator>  
#include <algorithm>  
#include <string>  
using   namespace   std;  

struct   Person{  
    string   name;
    string   add;
    Person(const string &_name,const string &_add):name(_name),add(_add){}
    };  
class   My_less{
    public:
        inline
        bool operator()(const Person *p1,const Person *p2)const{
            return   p1->name<p2->name;
            }
        };  

int main(int argc, char *argv[]){
    Person   *p1=new   Person("ddd","shanghai");
    Person   *p2=new   Person("bbb","nanjing");
    Person   *p3=new   Person("ccc","wuhan");  
           
    list<Person*>   coll;  
    list<Person*>::iterator   it;  
           
    coll.push_back(p1);  
    coll.push_back(p2);  
    coll.push_back(p3);  
           
    for(it=coll.begin();it!=coll.end();++it){
        cout<<(*it)->name<<"   "<<(*it)->add<<endl;
        }  
    coll.sort(My_less());  
    cout<<"-------"<<endl;  
    for(it=coll.begin();it!=coll.end();++it){
        cout<<(*it)->name<<"   "<<(*it)->add<<endl;
        }  
    for(int u =0; u <5; ++u){
        int x = u;
        cout<< " u = "<<u<< " x = "<<x<<endl;
        }
  system("PAUSE"); 
  return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值