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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制