Cpp中使用getline读取一整行字符串


getline(input,str,delim)

Parameters

input-the stream to get data from
str-the string to put the data into
delim-the delimiter character

Return value

input



注:

getline是在string头文件中定义的一个函数,以一个istream对象和一个string对象作为输入参数。该函数首先读取输入流的内容直到遇到换行符停止,然后将读入的数据存入string对象,最后返回istream对象。其中换行符读入但是不保留。即换行符'\n'作为默认delim的参数值。

详细可查:http://en.cppreference.com/w/cpp/string/basic_string/getline


代码示例:

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string buf;
    cout<<"Type your input:\n";
    getline(cin,buf);
    cout<<buf;
    return 0;
}
测试:

Type your input:
    Hello,World!     --By Dennis.Ritchie
    Hello,World!     --By Dennis.Ritchie


#include <string>
#include <iostream>
using namespace std;

int main()
{
    string buf;
    cout<<"Type your input:\n";
    getline(cin,buf,'.');
    cout<<buf;
    return 0;
}
测试:

Type your input:
    Hello,World!     --By Dennis.Ritchie
    Hello,World!     --By Dennis



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值