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++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

C++/类与对象/默认成员函数@构造函数的用法

《C++/类与对象/默认成员函数@构造函数的用法》:本文主要介绍C++/类与对象/默认成员函数@构造函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录名词概念默认成员函数构造函数概念函数特征显示构造函数隐式构造函数总结名词概念默认构造函数:不用传参就可以

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一