C++中字符串I/O

可以使用cout和插入操作符<<来输出string对象,这和处理其他类型的数据是一样的;

可以使用cin和提取操作符>>时,方式和其他数据一样,但要注意:>>会忽略最初的空白字符,并在遇到更多的空白字符时停止读入!

// String.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>

int main(int argc, char* argv[])
{
	using namespace std;
	string line;
	cout<<"Enter a line ofinput :";
	cin>>line;
	cout<<"Output :";
	cout<<line;
	cout<<endl;
	return 0;
}



如果要想读取一整行,可以使用getline()函数,注意:这个getline()不是成员函数!

// String.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>

int main(int argc, char* argv[])
{
	using namespace std;
	string line;
	cout<<"Enter a line ofinput :";
	getline(cin,line);
	cout<<"Output :";
	cout<<line;
	cout<<endl;
	return 0;
}


注意:此时未忽略前面的空白符!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值