请问如何将c++的string类型变量中的内容输出到控制台中,同时也能从控制台中输入?

蓝鹰 2012-01-25 02:17:37
我尝试过下面的代码,但是不行:

void main()
{
string a;
a="apple";
cout<<a<<endl; //错误提示说<<无法匹配该操作
}

请问有什么简单的方法能满足我的要求?急求大侠来帮忙!
...全文
640 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
dos5gw 2012-02-10
  • 打赏
  • 举报
回复
string line;
while (getline(cin, line))
蓝鹰 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 35 楼 dos5gw 的回复:]

string line;
while (getline(cin, line))
[/Quote]
what are you talking about?
Binzo 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 zhao4zhong1 的回复:]

摒弃cin、cout;
使用scanf、printf
[/Quote]
为什么呀。
蓝鹰 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 zhao4zhong1 的回复:]

摒弃cin、cout;
使用scanf、printf
[/Quote]
既然都用c++了,难道还得用字符数组吗?
IVERS0N 2012-02-07
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 yisikaipu 的回复:]

vc吧?#include <string>

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s;
cin >>s;
cout <<s <<endl;

return 0;
}
[/Quote]
++
赵4老师 2012-02-07
  • 打赏
  • 举报
回复
摒弃cin、cout;
使用scanf、printf
程序员小迷 2012-02-07
  • 打赏
  • 举报
回复
楼主你说的这个情况太诡异了;
标准string能不支持?
string头文件包含了么?
indiantom 2012-02-02
  • 打赏
  • 举报
回复
貌似是因为string类里面重载了包括"<<"在内的运算符


//重载运算符"<<"
ostream& operator<<(ostream &output, const String &other)
{
output<<other.m_data;

cout<<"重载了运算符<<"<<endl;

return output;
}
indiantom 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 blackeaglex 的回复:]

引用 26 楼 indiantom 的回复:
具体不清楚 只知道<string>里好像是是一个string类的定义......
等其他大大解答

我记得里面只有一些strcpy 和strcmp之类的函数
[/Quote]

那个是C的<string.h> C++里对应的头文件是<cstring>
可以搜下 string.h和string的区别
蓝鹰 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 indiantom 的回复:]
具体不清楚 只知道<string>里好像是是一个string类的定义......
等其他大大解答
[/Quote]
我记得里面只有一些strcpy 和strcmp之类的函数
indiantom 2012-02-02
  • 打赏
  • 举报
回复
具体不清楚 只知道<string>里好像是是一个string类的定义......
等其他大大解答
蓝鹰 2012-02-02
  • 打赏
  • 举报
回复
我再追加一点分数,有人能说说其中的原因吗?这与string头文件里面哪个函数有关呢?
indiantom 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 yisikaipu 的回复:]

vc吧?#include <string>

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main()
{
string s;
cin >>s;
cout <<s <<endl;

return 0;
}
[/Quote]


正解 经验证VC6 没错
蓝鹰 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 yisikaipu 的回复:]
再说一遍,vc下要加#include <string>
[/Quote]
不好意思,我刚才没看清楚。谢谢了
yisikaipu 2012-01-29
  • 打赏
  • 举报
回复
再说一遍,vc下要加#include <string>
蓝鹰 2012-01-29
  • 打赏
  • 举报
回复
我看到有人为了这个事情还重载操作符号>>和<<,那也太麻烦了吧!
我给string添加个成员函数好了
蓝鹰 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 gary_19872006 的回复:]
#14楼 应该是正解啊
char *p=new char(256);
cin >> p;
a.assign(p);
cout << a.c_str() << endl;
[/Quote]
说得没错,但我是希望用cin>>对string类的变量进操作
如果真的弄不好我干脆自己写一个string的派生类好了
yisikaipu 2012-01-28
  • 打赏
  • 举报
回复
vc吧?#include <string>

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s;
cin >>s;
cout <<s <<endl;

return 0;
}
gary_19872006 2012-01-28
  • 打赏
  • 举报
回复
std::cin std::cout 的 << >> 操作符不支持直接string操作
gary_19872006 2012-01-28
  • 打赏
  • 举报
回复
#14楼 应该是正解啊
char *p=new char(256);
cin >> p;
a.assign(p);
cout << a.c_str() << endl;
加载更多回复(16)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧