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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C