这是什么问题? [英] what is problem in this ?

查看:55
本文介绍了这是什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct node
{
    string bousha;
    node *next;
};

void main()
{
    string name;
    node *head=NULL;
    cout<<"Enter the name :"<<endl;
    cin>>name;
    node *temp;
    temp = (node*)malloc(sizeof(string));
    temp->bousha=name;
    temp->next=head;
    head=temp;

    cout<<"name is :"<<temp->bousha<<endl;
    getch();
}



为什么列表不能存储字符串?????



why the list can not storage string ?????

推荐答案

首先,(node*)malloc(sizeof(string));只是胡言乱语.不仅您使用了错误的分配方式(您将需要new),不仅您没有为node保留内存(node不包含字符串),而且不仅没有定义什么字符串(代码不会按原样编译),您不理解运算符sizeof无法考虑可变大小的数据(例如字符串),只是因为相同类型的不同实例占用的内存量不同,取决于字符串内容.

除了为您编写一些工作代码外,我不知道该如何帮助您,但这不是可以为您提供帮助的东西.我查看了您过去的问题-看来您不是在学习,只是在做一些无法帮助您学习的事情.自您提出第一个问题以来,您可能会花费大约四个月的时间,从而提高工作效率;获得一些非常基本的技能就足够了.相反,您不断询问此代码是什么意思?".您最终可以理解这根本没有效果吗?您应该自己编写所有代码,并且只编写您完全理解的代码行.最初,它可能非常简单,但后来您可以扩展您的曲目.使用您的猜测是不可能做到的,因此您需要阅读一些有关编程语言和基础知识的教科书,这是您所能找到的最简单的方法.如果您不着急就开始这样做,那么您很快就会发现很大的进步.

—SA
To start with, (node*)malloc(sizeof(string)); is just gibberish. Not only you use wrong way of allocation (you would need new), not only you do not preserve memory for node (node does not consist of the string along), not only you do not define what string (the code won''t compile as is), you don''t understand that the operator sizeof cannot take into account the variable-size data like string, just because the different instances of the same type take different amount of memory, depending on the string content.

I don''t know how to help here except writing some working code for you, but this is not what can help you. I took a look at your past questions — it looks like you are not learning, just doing something which cannot help you to learn. You could spend about four months since your first question in a more productive way; it would be quite enough to get some very basic skills. Instead, you keep asking "what is the mean this code?". Can you finally understand that this is not productive at all? You should write all the code by yourself, and only the lines you fully understand. At first, it can be very simple, but later on you can extend your repertoire. This is impossible to do using your guesswork, so you need to read some textbook on the language and basics of programming, the simplest you can find. If you start doing it without rushing yourself, you well see considerable progress really soon.

—SA


您应该使用C ++编写整个程序,而不是C或C ++中的其他程序.

也就是说,您应该使用new而不是malloc,或者甚至更好地使用STL列表(std :: list).
You should write your whole program in C++ and not part in C and other in C++.

That is, you should use new instead of malloc or even better uses STL list (std::list) instead.


真的不明白这是什么意思,您应该在开始对链接列表之类的复杂"数据结构进行编码之前,一定要阅读一些不错的C ++教程.最大的错误是分配,正如其他人指出的那样.
代替这个:
Really don''t understand what does this want to be, you should definitely check out some good C++ tutorials before you start coding ''complex'' data structures like linked lists. The biggest mistake is the allocation as others pointed out.
Instead of this:
node *temp;
temp = (node*)malloc(sizeof(string));


使用更多的C ++风格的内容:


use this that is more C++-ish:

node *temp = new node;



虽然我不是stl容器的忠实拥护者(并且我讨厌<<>>流运算符-*呕吐*),但最好还是从它们开始,直到您了解C ++和C ++的基本原理为止.那么您可以编写自己的数据结构,而这些数据结构没有sgi stl这样丑陋的源代码,并且对于您的特定问题更有效,并且对缓存更友好.



Although I''m not a big fan of the stl containers (and I hate the << and >> stream operators - *vomit*) maybe its better to start with them until you understand the basic principles of C++ and then you can write your own data structures that don''t have such an ugly source code as the sgi stl and are more efficient for your particular problems and are more cache friendly.


这篇关于这是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆