iterator反向遍历为什么会出错?

问题描述
给你两个非负实数A和B,如果A等于B,输出 "YES", 否则输出"NO"


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string a[100][2];
int n, cnt = 0;
bool ans[100];

void delete_0(string &a);

int main(){
    cin >> n;
    getchar();
    for (int i = 0; i < n;i++){
        string s;
        getline(cin,s);
        istringstream is(s);
        is >> a[i][0];
        is >> a[i][1];
        ////cout << a[i][0].back() << endl;
        //进行首位末位删0,但是如果是0.1就不需要删除
        delete_0(a[i][0]);
        delete_0(a[i][1]);
        cout << a[i][0] << ' ' << a[i][1] << endl;
        if(a[i][0]==a[i][1])
            ans[cnt++] = true;
        else
            ans[cnt++] = false;
    }
    for (int i = 0; i < cnt;i++){
        if(ans[i])
            cout << "YES";
        else
            cout << "NO";
        if(i<cnt-1)
            cout << endl;
    }
    system("pause");
    return 0;
}
//指向string类的迭代器。你可以理解为指针
void delete_0(string &a){
    //考虑特殊情况0.0 00.00 010.010 00 010 100
    string::iterator it;
    if(a.find('.')!=a.npos){
        //有小数点
        for (it = a.begin(); it!=a.end();){
            if(*it=='0'){
                it = a.erase(it);
            }
            else
                break;
        }
        //就是这里为什么debug时从'/000'之后就一直删除string中的元素没有停止
        // for (it = a.end(); it!=a.begin();){
        //     if(*it=='0'){
        //         a.pop_back();
        //         it--;
        //     }
        //     else
        //         break;
        // }
        while(a.back()=='0')
            a.pop_back();
    }else{//只需要删除前面的0即可
        for (it = a.begin(); it!=a.end();){
            if(*it=='0'){
                it = a.erase(it);
            }
            else
                break;
        }
        //再加入一个小数点
        a.push_back('.');
    }
}

for (it = a.end(); it!=a.begin();) *it永远取不到‘0’,直接break了


#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string a[100][2];
int n, cnt = 0;
bool ans[100];

void delete_0(string &a);

int main()
{
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++)
    {
        string s;
        getline(cin, s);
        istringstream is(s);
        is >> a[i][0];
        is >> a[i][1];
        ////cout << a[i][0].back() << endl;
        //进行首位末位删0,但是如果是0.1就不需要删除
        delete_0(a[i][0]);
        delete_0(a[i][1]);
        cout << a[i][0] << ' ' << a[i][1] << endl;
        if (a[i][0] == a[i][1])
            ans[cnt++] = true;
        else
            ans[cnt++] = false;
    }
    for (int i = 0; i < cnt; i++)
    {
        if (ans[i])
            cout << "YES";
        else
            cout << "NO";
        if (i < cnt - 1)
            cout << endl;
    }
    system("pause");
    return 0;
}
//指向string类的迭代器。你可以理解为指针
void delete_0(string &a)
{
    //考虑特殊情况0.0 00.00 010.010 00 010 100
    string::iterator it;
    if (a.find('.') != a.npos)
    {
        //有小数点
        for (it = a.begin(); it != a.end();)
        {
            if (*it == '0')
            {
                it = a.erase(it);
            }
            else
                break;
        }
        //就是这里为什么debug时从'/000'之后就一直删除string中的元素没有停止
        for (it = a.end() - 1; it != a.begin();)
        {
            if (*it == '0')
            {
                a.pop_back();
                it--;
            }
            else
                break;
        }
        while (a.back() == '0')
            a.pop_back();
    }
    else
    { //只需要删除前面的0即可
        for (it = a.begin(); it != a.end();)
        {
            if (*it == '0')
            {
                it = a.erase(it);
            }
            else
                break;
        }
        //再加入一个小数点
        a.push_back('.');
    }
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632