给结构体中string赋值时,程序被中断
来源:互联网 发布:淘宝网艾灸盒 编辑:程序博客网 时间:2024/05/22 07:58
只要把分配空间的malloc,改成new struct 就好
错误代码:
#include <iostream>#include <string>using namespace std;typedef struct st{ string s; int number; struct st* next;}node,*Pnode;void Trans(string &c);void InsertList(Pnode *List,string c);void Display(Pnode List);int main(){ int num; int ifin; string c; cin >> num; Pnode List; List = NULL; while(num--) { cin >> c; Trans(c); InsertList(&List,c); Display(List); } return 0;}void Trans(string &c){ for (string::iterator i=c.begin();i!=c.end();++i) { if ((*i)=='-') { i = c.erase(i); continue; } if (isupper(*i)) { (*i) = ((*i)-65)/3+49; } }}void InsertList(Pnode *List,string c){ Pnode p = (*List),q,pre; if (p==NULL) { p = (Pnode)malloc(sizeof(node)); p->s = c; p->number = 1; p->next = NULL; } else{ while(p) { if (c.compare(p->s)) { p->number++; break; } pre = p; p = p->next; } if (!p) { q = (Pnode)malloc(sizeof(node)); q->s = c; q->number = 1; q->next = NULL; pre->next = q; } }}void Display(Pnode List){ int num = 0; while(List) { if (List->number > 1) { num=0; for (string::iterator i=List->s.begin();i!=List->s.end();++i) { if (num == 3) cout << "-" << *i; else cout << *i ; num++; } cout << " " << List->number<< endl; } List = List->next; }}
正确代码:
#include <iostream>#include <string>using namespace std;typedef struct st{ string s; int number; struct st* next;}node,*Pnode;void Trans(string &c);void InsertList(Pnode *List,string c);void Display(Pnode List);int main(){ int num; int ifin; string c; cin >> num; Pnode List; List = NULL; while(num--) { cin >> c; Trans(c); InsertList(&List,c); Display(List); } return 0;}void Trans(string &c){ for (string::iterator i=c.begin();i!=c.end();++i) { if ((*i)=='-') { i = c.erase(i); continue; } if (isupper(*i)) { (*i) = ((*i)-65)/3+49; } }}void InsertList(Pnode *List,string c){ Pnode p = (*List),q,pre; if (p==NULL) { p = new node; p->s = c; p->number = 1; p->next = NULL; } else{ while(p) { if (c.compare(p->s)) { p->number++; break; } pre = p; p = p->next; } if (!p) { q = new node; q->s = c; q->number = 1; q->next = NULL; pre->next = q; } }}void Display(Pnode List){ int num = 0; while(List) { if (List->number > 1) { num=0; for (string::iterator i=List->s.begin();i!=List->s.end();++i) { if (num == 3) cout << "-" << *i; else cout << *i ; num++; } cout << " " << List->number<< endl; } List = List->next; }}
0 0
- 给结构体中string赋值时,程序被中断
- 给结构体中std::string成员变量赋值失败
- memset(&struct, 0, sizeof(struct))后给结构体中string赋值出错
- TextView给String赋值
- 数组直接赋值给结构体
- ios 给自定义结构体赋值
- 给char数组或者结构体赋值
- 结构体中字符串赋值
- 给结构体内数组赋值
- 给结构赋值和显示
- android ndk 给结构体赋值的方法
- android ndk 给结构体赋值的方法
- C++类 给结构体成员 函数指针 赋值
- 如何给结构体中的字符数组赋值。
- 给结构体里定义的二维数组赋值
- 以复杂结构体为参数在子函数中进行赋值,并返回值给主函数
- C中结构体赋值问题
- cuda中结构体的赋值
- Hadoop 学习笔记三 MapReduce
- Java内存模型及GC原理java内存模型 Java内存模型及GC原理 Java内存模型及GC原理 sun官方网站:sun java 虚拟机模型 Java内存模型及GC原理 JVM内存模型中分两
- word中插入罗马数字并且设置为Times New Roman字体
- 从0开始学习OC程序-第7天
- JS中的window.location.href解释
- 给结构体中string赋值时,程序被中断
- 坚果手机如何获取root权限
- 小学生四则运算的出题程序,这回用的是android写的
- Invalidate/InvalidateRect/UpdateWindow/RedrawWindow区别与联系
- 解读TCP 四种定时器
- 设计模式的六大原则学习
- 宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等
- @date2016-03-14 22:44(软件工程男的日常)
- CLOSE_WAIT状态的原因与解决方法