std::getline
来自cppreference.com
< cpp | string | basic string
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
定义于头文件 <string>
|
||
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input, |
(1) | |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input, |
(1) | (C++11 起) |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input, |
(2) | |
template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input, |
(2) | (C++11 起) |
getline
从输入流中读取字符,并把它们转换成字符串1)
的行为就像
UnformattedInputFunction
,除了input.gcount()
不会受到影响。在构造和检查岗哨对象的,执行以下操作:原文:
Behaves as
UnformattedInputFunction
, except that input.gcount()
is not affected. After constructing and checking the sentry object, performs the following:2)
a)
b)
c)
3)
2)
[编辑] 参数
input | - | |
str | - | |
delim | - |
[编辑] 返回值
input
[编辑] 示例
下面的代码要求用户输入他们的名字,然后迎接他们用这个名字.
运行此代码
#include <string> #include <iostream> int main() { std::string name; std::cout << "What is your name? "; std::getline(std::cin, name); std::cout << "Hello " << name << ", nice to meet you."; }
可能的输出:
What is your name? John Q. Public Hello John Q. Public, nice to meet you.