江火如流萤 2022-03-15 20:48 采纳率: 100%
浏览 27
已结题

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('.');
    }
}
  • 写回答

2条回答 默认 最新

  • 关注

    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('.');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月31日
  • 已采纳回答 3月23日
  • 创建了问题 3月15日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来