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

2023-12-04 04:32

本文主要是介绍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;}elsestd::cout<<"打开文本失败!\n";return 0;
}
/****************************************************
运行结果如下:
Please, enter your full name: wangshihui
Hello, wangshihui!
reading from TheMisteryMethod.txt:after getline (pua,afc,',') afc is:
To recapafter getline (pua,afc) afc is:
the three main objectives in the Mystery Method are:after getline (pua,afc,'斜杠0')  afc is:To attract a womanTo establish comfort, trust, and connectionTo 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::coutint 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: wangshihuiPlease, enter your favourite book: the mistery methodafter std::cin.getline (title,256) :wangshihui's favourite book is the mistery methodPlease, enter your favourite book again:
the mistery methodafter 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.*******************************************************/




这篇关于C++ 读书笔记之 getline与cin.getline的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/452098

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序