此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

getline 函数如下所示:

getline(cin, inputLine);

其中 cin 是正在读取的输入流,而 inputLine 是接收输入字符串的 string 变量的名称。下面的程序演示了 getline 函数的应用:

// This program illustrates using the getline function
//to read character data into a string object.
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
string name;
string city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Enter the city you live in: ";
getline(cin, city);
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return ;
}

关于输入getline的更多相关文章

  1. c++面向行的输入getline()和get()

    来源:c++ primer plus 在c++里当我们输入一个字符串时习惯用cin,但是cin只能读取一段不含空格的字符串,如果我们需要读取一段包含空格的字符串时,就需要用到getline()或get ...

  2. C++中的字符串输入getline

    http://www.cnblogs.com/wanghao111/archive/2009/09/05/1560822.html 1 #include <iostream> 2 #inc ...

  3. C plus plus primer plus

    1,运算符,操作数; 使用相同的符号(基本运算符,+ - * /  %),进行多种操作(指使用一个运算符符号,对不用类型的变量进行操作,int ,float,double),此概念将运算符的重载(op ...

  4. C++ Primer Plus 文章17章 进,输出和文件

    文章17章 进.输出和文件 1.当到达输入句子.他将刷新输出缓冲区满输出电流 2.streambuf分类 它提供了用于各种操作的一个缓冲 ios_base类表示流的一般特征 ios基础的类ios_ba ...

  5. 题解-洛谷P1184 高手之在一起

    https://www.luogu.org/problemnew/show/P1184 (题目出处) 见到地名,自然就想到字符串了.可以从第一天开始,将她的位置与高手方便取得地方一一比较,(char字 ...

  6. C++标准模板库(STL)介绍:string的基本用法

    1.带空格的字符串的输入 getline(cin, str)

  7. DS博客作业03—栈和队列

    1.本周学习总结 本周学习了栈和队列两种数据结构,分别对应后进先出,先进先出两种数据操作 学会栈的特殊类型-共享栈,队列的特殊类型-循环队列的一系列操作 学会熟练使用栈和队列的STL容器,使代码简洁 ...

  8. C++ Review

    #include "iostream" #include "iomanip" #include "cstdio" using namespa ...

  9. c++ primer plus 第6版 部分一 1-4章

    c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c   cc   cxx     C    cpp     ...

随机推荐

  1. oracle 11gR2 如何修改vip

    因为业务需要,需要将p570a主机和p570b主机上的vip做修改 修改前ip 192.168.128.12   p570a-vip 192.168.128.13   p570b-vip 修改后ip ...

  2. 6 ZigZig Conversion[M]Z字形变换

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  3. BZOJ 2120 带修莫队

    思路: 暴力能过的 嘿嘿嘿 我是来练带修莫队的嗯 复杂度 O(n^5/3) //By SiriusRen #include <cmath> #include <cstdio> ...

  4. POJ 2346 DP or打表

    这题 不算重复的数.. 就变成水题了. 思路: 1.打表 2.DP 打表的: // by SiriusRen #include <cstdio> using namespace std; ...

  5. CheckBox的Attributes

    在看老同事写的代码的时候,发现了这样的一段代码:之前自己没有遇到过,记录下吧. 大致是这样的 foreach (GridViewRow grv in GridView1.Rows) { CheckBo ...

  6. Windows环境下配置thrift编译器(VS2015)

        参考官方文档:http://svn.apache.org/repos/asf/thrift/attic/trunk/compiler/cpp/README_Windows.txt   编译器( ...

  7. Dynamic dispatch mechanisms

    Normally, in a typed language, the dispatch mechanism will be performed based on the type of the arg ...

  8. day07 分支,循环

    目录 if(分支) if的语法 if...else... if...elif...else if的嵌套 for循环 for-else 语句 for循环的嵌套(重要) range介绍 while循环 w ...

  9. Codeforces Round #493 (Div. 2) A. Balloons 贪心水题

    由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...

  10. Label,PushButton,ToolButton 实现动态图片按钮,Label显示gif动画

    .h文件 public: explicit event(QWidget *parent = 0); ~event(); QImage image; QLabel *label; QLabel *lab ...