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++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav