C++ 读书笔记之 getline与cin.getline的区别

两个函数虽然看上去名称相同都是getline,但它们却分属于不同的类中的成员函数。
cin.getline(arr,20);的getline是输入流对象的成员函数,即istream::getline,使用时需头文件#include<iostream>
getline(cin,str);的getline是string类对象的成员函数,即string::getline,使用时需头文件#include <string>


为测试程序结果,令文本文件TheMisteryMethod.txt文件内容如下:

To recap,the three main objectives in the Mystery Method are:
            To attract a woman
            To establish comfort, trust, and connection
            To structure the opportunity to be seduced



getline

std::getline (string)

(1)
istream& getline (istream& is, string& str, char delim);
(2)
istream& getline (istream& is, string& str);
Get line from stream into string
Extracts characters from is and stores them intostr until the delimitation character delim is found (or the newline character,'\n', for (2)).

The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

Each extracted character is appended to the string as if its member push_back was called.

Parameters

is
istream object from which characters are extracted.
str
string object where the extracted line is stored.

Return Value

The same as parameter is.

example of getline

// extract to string
#include <iostream>
#include <string>
#include <fstream>

int main ()
{
    std::string name;

    std::cout << "Please, enter your full name: ";
    std::getline (std::cin,name);
    std::cout << "Hello, " << name << "!\n";
    std::cout <<"reading from TheMisteryMethod.txt:\n";
    std::ifstream pua("TheMisteryMethod.txt");
    if(pua.is_open())
    {
        std::string afc;
        std::getline (pua,afc,',');//以逗号作为分隔结束符
        std::cout<<"\nafter getline (pua,afc,',') afc is:\n";
        std::cout<<afc<<'\n';

        std::getline (pua,afc);//默认换行为分割结束符
        std::cout<<"\nafter getline (pua,afc) afc is:\n";
        std::cout<<afc<<'\n';

        std::getline (pua,afc,'\0');//以C风格字符串结束标志作为分割结束符
        std::cout<<"\nafter getline (pua,afc,'斜杠0')  afc is:\n";
        std::cout<<afc;
    }
    else
        std::cout<<"打开文本失败!\n";

    return 0;
}
/****************************************************
运行结果如下:
Please, enter your full name: wangshihui
Hello, wangshihui!
reading from TheMisteryMethod.txt:

after getline (pua,afc,',') afc is:
To recap

after getline (pua,afc) afc is:
the three main objectives in the Mystery Method are:

after getline (pua,afc,'斜杠0')  afc is:
            To attract a woman
            To establish comfort, trust, and connection
            To structure the opportunity to be seduced
Process returned 0 (0x0)   execution time : 3.345 s
Press any key to continue.

*****************************************************/


cin.getline


std::istream::getline

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Get line
Extracts characters from the stream as unformatted input and stores them intos as a c-string, until either the extracted character is the delimiting character, orn characters have been written to s (including the terminating null character).

The delimiting character is the newline character ('\n') for the first form, anddelim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written tos.

The function will also stop extracting characters if the end-of-file is reached. If this is reached prematurely (before either writingn characters or finding delim), the function sets the eofbit flag.

The failbit flag is set if the function extracts no characters, or if thedelimiting character is not found once (n-1) characters have already been written tos. Note that if the character that follows those (n-1) characters in the input sequence is precisely thedelimiting character, it is also extracted and the failbit flag is not set (the extracted sequence was exactlyn characters long).

A null character ('\0') is automatically appended to the written sequence ifn is greater than zero, even if an empty string is extracted.


This function is overloaded for string objects in header<string>: See getline(string).

Parameters

s
Pointer to an array of characters where extracted characters are stored as a c-string.
n
Maximum number of characters to write to s (including the terminating null character).
If the function stops reading because this limit is reached without finding the delimiting character, the failbit internal flag is set.
streamsize is a signed integral type.
delim
Explicit delimiting character: The operation of extracting successive characters stops as soon as the next character to extract compares equal to this.

Return Value

The istream object (*this).


example of cin.getline

// istream::getline example
#include <iostream>     // std::cin, std::cout

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

    std::cout << "Please, enter your name: ";
    std::cin.getline (name,256);

    std::cout << "\nPlease, enter your favourite book: ";
    std::cin.getline (title,256);
    std::cout<<"\nafter std::cin.getline (title,256) :\n ";
    std::cout << name << "'s favourite book is " << title<<'\n';

    std::cout << "\nPlease, enter your favourite book again: \n";
    std::cin.getline (title,256,' ');
    std::cout<<"\nafter std::cin.getline (title,256,' ') :\n ";
    std::cout << name << "'s favourite book is " << title;

    return 0;
}
/******************************************************
运行结果如下:
Please, enter your name: wangshihui

Please, enter your favourite book: the mistery method

after std::cin.getline (title,256) :
 wangshihui's favourite book is the mistery method

Please, enter your favourite book again:
the mistery method

after std::cin.getline (title,256,' ') :
 wangshihui's favourite book is the
Process returned 0 (0x0)   execution time : 19.729 s
Press any key to continue.

*******************************************************/




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值