字符串的读入

可读空格:
cin.get();
cin.getline();
gets();
getline();

前三istring流 最后一个string流

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f
#define bug(x) cout<<"#"<<x<<endl;
#define bug2(a,b) cout<<"#"<<a<<" "<<"#"<<b;
void test1()
{
    /*
    gets();
    只能读C语言的字符串(istream流)
    可以读空格,遇到回车读入结束
    可以读二维的
    */
    char a[20];
    gets(a);
    bug(a);

    char b[20][20];
    for(int i=1;i<=3;i++)
        gets(b[i]);
    for(int i=1;i<=3;i++)
        bug(b[i]);
}
void test2()
{
    /*
    cin.get(str,n);
    (1)
    istring流
    可读空格
    在读字符串上理解为和cin.getline()一样
    (2)
    读字符  输入一行字符串,只读一个

    */
    char s[20];
    cin.get(s,20,'s');
    bug(s);
    char c=cin.get();
    bug(c);

}
void test3()
{
    /*
    cin.getline(str,n,bj);
    是 istring流
    可以读空格
    第二个参数定义了读几个字符,可以少于这个数字,但不能大于!!
    第三个参数定义了读到哪个字符停止,默认回车'\n'
    可读二维
    */
    char a[20];
    cin.getline(a,20);
    bug(a);


    char b[5][20];
    for(int i=1;i<=3;i++)
        cin.getline(b[i],5);
    for(int i=1;i<=3;i++)
        bug(b[i]);
}
void test4()
{
    /*
    getline(cin,string)
    stirng 流
    可读空格
    不可读二维
    */
    string s;
    getline(cin,s);
    bug(s);
}
void test5()
{
    /*
    回车也吸收
    */
    char x;
    x=getchar();
    bug(x);
    x=getchar();
    bug(x);
}
int main()
{
    //test1();
    //test2();
    //test3();
    //test4();
    test5();
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值