目录

getline

描述 (Description)

它用于从流中提取字符作为未格式化的输入,并将它们作为c字符串存储到s中,直到提取的字符是分隔字符,或者n个字符已写入s(包括终止空字符)。

声明 (Declaration)

以下是std :: basic_istream :: getline的声明。

basic_istream& getline (char_type* s, streamsize n );
basic_istream& getline (char_type* s, streamsize n, char_type delim);

参数 (Parameters)

  • s - 指向一个字符数组的指针,其中提取的字符存储为c字符串。

  • n - 要写入s的最大字符数(包括终止空字符)。

  • delim - 显式分隔字符:提取连续字符的操作在提取的下一个字符与此相等时立即停止(使用traits_type :: eq)。

返回值 (Return Value)

返回basic_istream对象(* this)。

异常 (Exceptions)

Basic guarantee - 如果抛出异常,则对象处于有效状态。

数据竞争 (Data races)

修改由s和流对象指向的数组中的元素。

例子 (Example)

在下面的示例中为std :: basic_istream :: getline。

#include <iostream>
int main () {
   char name[256], title[256];
   std::cout << "Please, enter your name: ";
   std::cin.getline (name,256);
   std::cout << "Please, enter your favourite movie: ";
   std::cin.getline (title,256);
   std::cout << name << "'s favourite movie is " << title;
   return 0;
}

输出应该是这样的 -

Please, enter your name: iowiki
Please, enter your favourite movie: ted
iowiki's favourite movie is ted 
↑回到顶部↑
WIKI教程 @2018