现在的位置: 首页 > 综合 > 正文

getline函数的几种用法

2013年12月08日 ⁄ 综合 ⁄ 共 663字 ⁄ 字号 评论关闭
文章目录
(1)istream& getline ( istream& is, string& str, char delim );
     istream& getline ( istream& is, string& str );
Get line from stream

Example

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
 string str, line;
 //string* test;
 while(getline(cin, line))
 {
  istringstream stream(line);
  while(stream>>str)
   cout<<str<<endl;
 } 
 return 0;
}
 
(2)istream& getline (char* s, streamsize n );
     istream& getline (char* s, streamsize n, char delim );
Get line from stream

Example

// istream getline
#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}

抱歉!评论已关闭.