getline(cin,_string); [英] getline(cin,_string);

查看:74
本文介绍了getline(cin,_string);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道getline(cin,_string);效果很好

但这不是:

char* _chArr = new char;
getline(cin,_chArr);

即使这也不起作用:

char* _chArr = new char[30];
getline(cin,_chArr);

不是char*字符串吗?

推荐答案

从逻辑上考虑一下. char *只是指向字符类型存储块的指针.您必须为其分配一些动态内存,然后使用strcpy()或手动将数据复制到其中. C ++不支持直接输入.实际上,字符串是对象,其内部包含大小.它们是由该行业的专家设计的,并且像内置功能一样提供了直接的输入和动态的增长. 字符串和字符串之间有区别. Cstring是char *.

Well think of it logically. the char* is just a pointer to a character type memory block. You have to assign it some amount of dynamic memory and then copy data into it using strcpy() or manually. Direct input is not supported in C++. Strings are in fact objects which contain size within themselves. They are designed by the experts in this industry, and they have provided the direct input and dynamic growth as in built functionality. There is a differnce between string and cstring. Cstring is the char*.

这篇关于getline(cin,_string);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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