首页 > 编程语言 > 详细

C++读入带空格的字符串

时间:2019-06-03 10:05:31      阅读:102      评论:0      收藏:0      [点我收藏+]
#include <stream>
#include <string>

int main()
{
    string str;
    getline(cin,str); //getline需包含<string>
    cout << str << endl; 
    return 0;  
}    

一些其他的相关函数

1.scanf() 头文件#include <stdio.h>

#include <stdio.h>
int main()
{
    char str[50];
    scanf("%[^\n]",str); //scanf("%s", str);将按空格分割读入
    printf("%s",str);
    return 0;
}

2.cin #include <iostream>

遇到空格或回车结束

3.gets() 

读到回车结束,在C++中运行会有bug提示,C++11已经抛弃这个函数,不建议使用。

4.getline() #include <string>

getline(cin,str);

5.cin.get(*char,int maxnum)

int main()
{
    char str[50];
    cin.get(str,20);
    cout << str;
    return 0;
}

6.cin.getline(*char,int maxnum)  #include <string>

int main()
{
    char str[50];
    cin.getline(str,20);
    cout << str;
    return 0;
}

 

C++读入带空格的字符串

原文:https://www.cnblogs.com/didiaoxiaoguai/p/10965820.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!