前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++ cin, get学习笔记

c++ cin, get学习笔记

作者头像
LRainner
发布2020-07-17 11:32:54
7530
发布2020-07-17 11:32:54
举报
文章被收录于专栏:安全学习笔记安全学习笔记

1.cin>>

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
        //输入数字,自动跳过不可见字符(如 空格 回车,Tab 等)
        int a, b;
        cin >> a >> b;  //1 3
        cout << a << endl;  //1
        cout << b << endl;  //3

        //输入一个字符串,遇到“空格 回车 Tab”都结束
        char c[20], d[20];
        cin >> c >> d;  //12 34 56
        cout << c << endl;  //12
        cout << d << endl;  //34
}

2.cin.get()

用来接收字符,字符数组,可以接收空格

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
        //cin.get(字符变量名) 可以用来接收字符
        //字符变量 = cin.get();
        //可以接收空格
        //cin.get(无参数)主要是用于舍弃输入中的不需要的字符,或者舍弃回车
        char c, d;
        //输入1 2
        c = cin.get();
        d = cin.get();
        cout << c << endl;  //1
        cout << d << endl;  //

        cin.get();  //过滤输入的2
        cin.get();  //过滤回车

        //cin.get(字符数组,接收的字符数) 用来接收一行字符串可以接收空格
        char s[20];
        //输入12 456789123 45
        cin.get(s, 10);
        cout << s << endl;  //12 456789
}

3.cin.getline()

cin.getline(字符数组,接收字符数) 接收一个字符串 可以接收空格

代码语言:javascript
复制
#include<iostream>
using namespace std;
int main()
{
        //cin.getline(字符数组,接收字符数) 接收一个字符串 可以接收空格
        char a[20];
        //输入12 3456789
        cin.getline(a, 20);
        cout << a << endl;  //12 3456789
}

4.getline(cin,string)

接收一个可以包含空格的string类字符串,需要包含头文件#include <string>

代码语言:javascript
复制
#include<iostream>
#include<string>
using namespace std;
int main()
{
        //getline(cin,string) 接收包含空格的string类字符串
        string a;
        //输入12 3456789
        getline(cin, a);
        cout << a << endl;  //12 3456789
}

5.gets()

gets(字符数组) 接收包含空格的字符串,需要包含头文件#include <string>

代码语言:javascript
复制
#include<iostream>
#include<string>
using namespace std;
int main()
{
        //gets(字符数组) 接收包含空格的字符串
        //类似getline()
        char a[20];
        //输入12 34
        gets(a);
        cout << a << endl;  //12 34
}

6.getchar()

getchar() 接收一个字符,需要包含头文件#include<string>

代码语言:javascript
复制
#include<iostream>
#include<string>
using namespace std;
int main()
{
        //getchar() 接收一个字符,需要包含头文件#include<string>
        //可以接收空格
        char a;
        //输入1
        a = getchar();
        cout << a << endl;  //1
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-01-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小白也编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档