Cons. Marx 2021-08-25 11:34 采纳率: 92.3%
浏览 42
已结题

为什么字符串赋值是错的?

img
请问为什么字符串不能直接赋值,编译过了,运行是错的


#include <iostream>
using namespace std;
void swap(struct List &a,struct  List &b);
#include <string>
#include<cstring> 

//            00100 6 4        
//00100 1 12309    00000 4 33218
//12309 2 33218    33218 3 12309
//33218 3 00000    12309 2 00100
//00000 4 99999    00100 1 99999
//99999 5 68237    99999 5 68237
//68237 6 -1    68237 6 -1

struct List{
    string add;
    int value;
    string next;
};

int main()
{
    string headadd;
    int n,node;
    cin>>headadd>>n>>node;
    List a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i].add>>a[i].value>>a[i].next;
    }
    for(int j=1;j<n;j++)
    {
        for(int k=j;k<n;k++)
        {
            if(a[k].add == headadd)
            {
                swap(a[k],a[0]);
            }
        }
    }
    for(int j=0;j<n;j++)
    {
        for(int k=j+1;k<n;k++)
        {
            if(a[k].add == a[j].next)
            {
                swap(a[k],a[j+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<a[i].value<<endl;
    }
    cout<<"here:"<<endl;
    //node=4,n=6
    for(int cnt=0;cnt<n/node;)//控制有几次循环 
    {
        for(int jud=cnt;jud<node/2;jud++)//控制每次循环的开始位置以及循环次数 
        {
            swap(a[jud],a[node-jud-1]);
        }
        cnt += node;
    }
    
    for(int i=0;i<n;i++)//遍历链表并将链表的下一个地址改变 
    {
        a[i].next = a[i+1].add;
    }
    a[n-1].next = '-1';    
    for(int i=0;i<n;i++)
    {
        cout<<a[i].add<<" "<<a[i].value<<" "<<a[i].next<<endl;
    }
}

void swap(struct List &a,struct  List &b)
{
    struct List L;
    L.add = a.add;
    L.value = a.value;
    L.next = a.next;
    a.add = b.add;
    a.value = b.value;
    a.next =b.next;
    b.add = L.add;
    b.value = L.value; 
    b.next = L.next;    
    
}
  • 写回答

2条回答 默认 最新

  • StjpStjp 2021-08-25 11:49
    关注
    如果我的回答对你有帮助,请点击采纳按钮,谢谢

    因为你这是字符串赋值,
    并且-1是两个字符组成的字符串
    (‘-’+‘1’=“-1”)
    所以赋值的时候
    要把你的代码的71行

    a[n-1].next = '-1';
    
    

    改为

    a[n-1].next = "-1";
    
    

    才行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月27日
  • 已采纳回答 9月19日
  • 创建了问题 8月25日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程