C++读取字符串数据的两种方式

时间:2021-01-25 22:04:04

C++读取字符串数据的两种方式

对于同样的样例输入:

ladder came tape soon leader acme RIDE lone Dreis peat

ScAlE orb eye Rides dealer NotE derail LaCeS DrIed

noel dire Disk mace Rob dries

方式1

#define LOCAL
#include<iostream>
using namespace std;
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif string s;
while(cin>>s){
cout<<s<<endl;
}
}

样例输出

ladder

came

tape

soon

leader

acme

RIDE

lone

Dreis

peat

ScAlE

orb

eye

Rides

dealer

NotE

derail

LaCeS

DrIed

noel

dire

Disk

mace

Rob

dries

方式2

#define LOCAL
#include<iostream>
using namespace std;
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif string s;
while(getline(cin,s)){
cout<<s<<endl;
}
}

样例输出:

ladder came tape soon leader acme RIDE lone Dreis peat

ScAlE orb eye Rides dealer NotE derail LaCeS DrIed

noel dire Disk mace Rob dries