我所写的一段,出现了string iterator incompitable,但不知道错在哪里,求助!

penguinsmith 2016-06-21 03:56:15


调试发生错误的语句为findterm函数中断点所示的位置。
findterm的函数的参数string s为45x5+23x3-12x+1这样形式的一段字符串(我调试时用的即左侧给出的这一段),i,j分别为s的iterator,在调用这个函数之初,i与j是相等的。
其中find_if的predicator是ifoprt,即第二张图片给出。
运行到第一个find_if时,调试报错,string iterator incompatible.

没有搞清楚是哪里错了,也不知道该怎么去找错误。
希望能得到指点。
...全文
172 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
penguinsmith 2016-06-22
  • 打赏
  • 举报
回复
引用 6 楼 lunat 的回复:
只有属于同一个容器的迭代器才能相互比较。(对于end存在例外) string s在传参的过程中经过了复制,不再是i,j所属的容器了。
谢谢!
lunat 2016-06-22
  • 打赏
  • 举报
回复
只有属于同一个容器的迭代器才能相互比较。(对于end存在例外) string s在传参的过程中经过了复制,不再是i,j所属的容器了。
penguinsmith 2016-06-21
  • 打赏
  • 举报
回复
引用 4 楼 lunat 的回复:
讨厌贴的代码图片!!我要改你的代码,抄都不好抄。 最简单的改法是把最后string s改成string &s就可以了。 你代码里面没有改i,j,s,所以最好用const的。

string findterm(string::const_iterator i, string::const_iterator j, const string & s); 
太神奇啦!真的改成string&就行了耶!!(不过我有修改j) 能否再麻烦解释一下原理呢?
lunat 2016-06-21
  • 打赏
  • 举报
回复
讨厌贴的代码图片!!我要改你的代码,抄都不好抄。 最简单的改法是把最后string s改成string &s就可以了。 你代码里面没有改i,j,s,所以最好用const的。

string findterm(string::const_iterator i, string::const_iterator j, const string & s); 
penguinsmith 2016-06-21
  • 打赏
  • 举报
回复
引用 2 楼 lovesmiles 的回复:
把完整的测试代码发上来看一下,我没有发现报错。个人估计你的iterator 不是同一个string的。
完整代码如下(源代码中在断点之后才会运行到的部分太多,于是我用/**/注释掉了。暂时不敢完全肯定所以供各位自行判断) // ForExecise.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<vector> #include<iostream> #include<algorithm> #include<string> #include<iterator> using namespace std; struct term { double coef; int power; }; struct Poly { vector<term> poly; }; //Read data bool ifoprt(char c) { return c == '+' || c == '-'; } bool ifx(char c) { return c == 'x'; } bool ifnotnum(char c) { return ifoprt(c) || ifx(c); } double strtof(string s) { const char* spt = s.c_str(); return atof(spt); } int strtoi(string s) { const char* spt = s.c_str(); return atoi(spt); } string findterm(string::iterator& i, string::iterator& j, string s)//abstract the string after begin() or an oprt, and before end() or an oprt; { //i isn't the end of s; //i denote the last port or begin(); //j=i this time, but we should ajust j to the next oprt or end(); string ret; if (i == s.begin()) //if i = begin(), i is not a oprt j = find_if(s.begin(), s.end(), ifoprt); else//i is the last oprt j = find_if(i + 1, s.end(), ifoprt);//oprt can't be the last character, so i+1 will not be the end of s ret = string(i, j); return ret; } term splitcp(string s)//input a string of a term, return the term { term ret; string::iterator it_x; it_x = find(s.begin(), s.end(), 'x'); if (it_x == s.end())//no x { ret.coef = strtof(s);//if no x, the whole string means the coef ret.power = 0; return ret; } //if running here, there is x in the term if (it_x == s.begin())//no coef ret.coef = 1; else ret.coef = strtof(string(s.begin(), it_x));//[beging,x) if (it_x + 1 == s.end())//if no power ret.power = 0; else ret.power = strtoi(string(it_x + 1, s.end())); return ret; } Poly read() { Poly ret; string s; string::iterator i, j; getline(cin, s); i = j = s.begin(); //invariable: have read characters in [original i, current i) //i marks the begining of each term while (i != s.end()) { //i=j=end is impossible here term t;//not end: there must be a new term string sfort = findterm(i, j, s); t = splitcp(sfort); ret.poly.push_back(t); i = j; } return ret; } //output int output(Poly& P) { int count = 0; for (vector<term>::size_type i = 0; i < P.poly.size(); i++) { if (P.poly[i].coef == 0) continue; if (count != 0 && P.poly[i].coef>0) cout << '+'; if (P.poly[i].coef == -1) cout << '-'; else if (P.poly[i].power == 0 || P.poly[i].coef != 1) cout << P.poly[i].coef; if (P.poly[i].power != 0) { cout << 'x'; if (P.poly[i].power != 1) cout << P.poly[i].power; } count++; } cout << endl; return 0; } /* //assistance bool compare(term a, term b) { return a.power > b.power; } //calculate Poly Plus(Poly& A, Poly& B) { Poly ret; vector<term>::size_type i, j, sA, sB; i = j = 0; sA = A.poly.size(); sB = B.poly.size(); //invariable:have done with i in A and j in B while (i != sA&&j != sB) { //i-not-endofA || j-not-endofB term t; if (j == sB || A.poly[i].power > B.poly[j].power) { t.coef = A.poly[i].coef; t.power = A.poly[i].power; i++; } else if (i == sA || A.poly[i].power < B.poly[j].power) { t.coef = B.poly[j].coef; t.power = B.poly[j].power; j++; } else if (A.poly[i].power == B.poly[j].power) { t.power = A.poly[i].power; t.coef = A.poly[i].coef + B.poly[j].coef; i++; j++; } ret.poly.push_back(t); } return ret; } Poly Minus(Poly& A, Poly& B) { Poly ret; vector<term>::size_type i, j, sA, sB; i = j = 0; sA = A.poly.size(); sB = B.poly.size(); //invariable:have done with i in A and j in B while (i != sA&&j != sB) { //i-not-endofA || j-not-endofB term t; if (j == sB || A.poly[i].power > B.poly[j].power) { t.coef = A.poly[i].coef; t.power = A.poly[i].power; i++; } else if (i == sA || A.poly[i].power < B.poly[j].power) { t.coef = B.poly[j].coef*(-1); t.power = B.poly[j].power; j++; } else if (A.poly[i].power == B.poly[j].power) { t.power = A.poly[i].power; t.coef = A.poly[i].coef - B.poly[j].coef; i++; j++; } ret.poly.push_back(t); } return ret; } Poly Multiply(Poly& A, Poly& B) { Poly ret; vector<term>::size_type sA, sB; sA = A.poly.size(); sB = B.poly.size(); for (vector<term>::size_type i = 0; i != sA; i++) { for (vector<term>::size_type j = 0; j != sB; j++) { double coef = A.poly[i].coef * B.poly[j].coef; int power = A.poly[i].power + B.poly[j].power; if (ret.poly.size() != 0)//poly不为空才去查找 { int n = 0; while (n != ret.poly.size() && ret.poly[n].power != power) n++; if (n != ret.poly.size()) { ret.poly[n].coef += coef;//查找到相同次数的,直接系数相加 continue; } } term t; t.coef = coef; t.power = power; ret.poly.push_back(t);//若poly为空,或者不为空但没查找到,新增一项 } } sort(ret.poly.begin(), ret.poly.end(), compare); return ret; } */ int main() { Poly A, B; A = read(); B = read(); output(A); output(B); /* Poly Pls, Mns, Mltp; Pls = Plus(A, B); output(Pls); Mns = Minus(A, B); output(Mns); Mltp = Multiply(A, B); output(Mltp);*/ return 0; }
勤奋的小游侠 2016-06-21
  • 打赏
  • 举报
回复
把完整的测试代码发上来看一下,我没有发现报错。个人估计你的iterator 不是同一个string的。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧