注册 登录
编程论坛 C++教室

求助vector iterator not dereferencable

阿龙0403 发布于 2008-10-17 21:50, 4707 次点击
我看的是C++primer的课后答案,程序如下。运行时出现 vector iterator not dereferencable 请求帮忙解决!多谢!!!

#include<iostream>
#include<vector>
using namespace std;
bool is_equal(vector<int> &v1,vector<int>&v2)
{ for(vector<int>::const_iterator i=v1.begin(),j=v2.begin() ;i!=v1.end(),j!=v2.end();i++,j++)
  { if(*i!=*j)
   {return false;
    break;
   }
  }
 return true;
}


int main()
{ int str1[6]={1,2,3,4,4,5};
  int str2[8]={1,2,3,4,4,5,6,5};
  int str3[5]={1,2,3,1,4,};
  vector<int> vstr1(str1,str1+6);
  vector<int> vstr2(str2,str2+8);
  vector<int> vstr3(str3,str3+4);
  if(is_equal(vstr1,vstr2))
   cout << "vstr1 and vstr2 have the same part!!!"<<endl;
  else cout <<"vstr1 and vstr2 have nothing in common!!!"<<endl;
 if(is_equal(vstr1,vstr3))
   cout << "vstr1 and vstr3 have the same part!!!"<<endl;
  else cout <<"vstr1 and vstr3 have nothing in common!!!"<<endl;
 if(is_equal(vstr3,vstr2))
   cout << "vstr2 and vstr3 have the same part!!!"<<endl;
  else cout <<"vstr2 and vstr3 have nothing in common!!!"<<endl;
 return 0;
}
5 回复
#2
debroa7232008-10-17 22:55
你用的什么编译平台?
这样做试试,
bool is_equal(vector<int> &v1,vector<int>&v2)
{
    vector<int>::const_iterator i=v1.begin();
    vector<int>::const_iterator j=v2.begin() ;
    for(;i!=v1.end(),j!=v2.end();++i,++j)
    {
        if(*i!=*j)
        {
            return false;
        }
    }
    return true;
}
#3
newyj2008-10-17 23:00
用dev-c++编译  能运行
bool is_equal(vector<int> &v1,vector<int>&v2)中的vector<int>&v2少空格吧?
#4
caoxiongwei122008-10-18 00:14
vector<int> vstr1(str1,str1+6);看这里有没有错
#5
sunkaidong2008-10-18 18:50
;i!=v1.end()&&j!=v2.end();
#6
阿龙04032008-10-18 21:08
非常谢谢, 问题解决了!!!
1