欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-03-30 20:34:37
C++读取字符串数据的两种方式 对于同样的样例输入: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS DrIed noel dire Disk ma ......

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