草庐IT

basic_istream

全部标签

c++ - 为什么 std::istream_iterator 忽略换行符?

我有以下代码:#include#include#includeintmain(){std::stringstreamstr;strit(str),end;for(;it!=end;++it){std::cout输出是:[abcdef][97][98][99][100][101][102]为什么std::istream_iterator忽略换行符? 最佳答案 因为istream_iterator使用operator>>。并且istream::operator>>(char)会跳过空格,除非您取消设置流的skipws标志。(例如使用no

c++ - C++17 std::basic_string_view 是否会使 C 字符串的使用无效?

C++17正在引入std::basic_string_view,它是非拥有字符串版本,其类仅存储指向字符串第一个元素的指针和字符串的大小。还有理由继续使用C字符串吗? 最佳答案 IstherestillareasontokeepusingCstrings?我认为可以公平地说,除了使用CAPI之外,从来没有有理由使用C字符串。在设计只需要字符的只读表示的函数或方法的接口(interface)时,您会更喜欢std::string_view。例如。搜索字符串、生成大写拷贝、打印它等等。在设计一个接受字符串拷贝的接口(interface)时

Learn the basics of Python 3-Chapter 7: Modules

1.ModulesPythonIntroductionIntheworldofprogramming,wecarealotaboutmakingcodereusable.Inmostcases,wewritecodesothatitcanbe reusableforourselves.Butsometimeswesharecodethat’shelpfulacrossabroadrangeofsituations. Inthislesson,we’llexplorehowtousetoolsotherpeoplehavebuiltinPythonthatarenotincludedautoma

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

我无法理解这个错误。这个错误不在我正在调试的类中。(是吗?)错误是:c:\programfiles\microsoftvisualstudio10.0\vc\include\fstream(890):errorC2248:'std::basic_ios::basic_ios':cannotaccessprivatememberdeclaredinclass'std::basic_ios'1>with1>[1>_Elem=char,1>_Traits=std::char_traits1>]1>c:\programfiles\microsoftvisualstudio10.0\vc\inc

c++ - 使用 std::ifstream、std::istream_iterator 和 std::copy 不读取整个文件

我在一个188字节的文件中使用了以下代码:std::ifstreamis("filename",std::ios::binary);std::vectorbuffer;std::istream_iteratori_input(is);std::copy(i_input,std::istream_iterator(),std::back_inserter(buffer));std::cout但是它只读取了188个字节中的186个字节。我已经在十六进制编辑器和ls-al中确认了文件大小。 最佳答案 我不知道为什么,但默认情况下似乎会跳过

c++ - getline 和 std::istream::operator>>() 有什么区别?

#include#includeusingnamespacestd;intmain(){stringusername;cout>username;}所以我很好奇这两个代码之间有什么区别,我听说这是同一件事,但如果是,那为什么要用两种方法呢?#include#includeusingnamespacestd;intmain(){stringusername;cout 最佳答案 区别在于std::getline—顾名思义—从给定的输入流(可能是std::cin)和operator>>中读取行读一个单词1。即std::getline读取直

c++ - 为什么 C++ 不为枚举类型提供默认的 "operator>>"函数?

我发现C++提供了默认的operatorenum的函数类型:#includeusingnamespacestd;enumOpType{Select,Insert};intmain(){OpTypet=Select;cout运行结果为:0虽然不提供默认operator>>功能:#includeusingnamespacestd;enumOpType{Select,Insert};intmain(){OpTypet=Select;cin>>t;return0;}构建它会产生以下编译错误:prog.cpp:Infunction‘intmain()’:prog.cpp:11:6:error:n

c++ - 如何将 'fixed' floatfield 用于 istream/istringstream?

C++有一个名为“fixed”的I/O操纵器,用于以固定(非科学)形式输入/输出float。它适用于输出,但我不明白如何让输入正常工作。考虑这个例子:#include#includeusingnamespacestd;intmain(){doublevalue;istringstream("1.4e1")>>fixed>>value;cout在我看来,它应该是这样工作的。输入流有一些字符串。当我们在其上应用fixed操纵器并尝试读取double/float时,它应该在第一个不是数字或点的字符处停止(第二次/第三次/更多次不接受点).因此,正确的输出将是1.4(当我们遇到'e'时我们停止

c++ - 如何为自定义 istream/streambuf 实现 seekg()?

十年前,我曾经是一名C++专家,但在过去的10年里,我一直在编写Java。我刚刚启动了一个使用小型第三方XML解析器的C++项目。XML解析器接受STListream。我的XML数据来自WindowsCOMIStream。我认为我会做正确的事并创建一个适配器来获取IStream数据并通过istream将其呈现给XML解析器。我遵循了http://www.mr-edd.co.uk/blog/beginners_guide_streambuf上的优秀教程并创建了一个从底层COMIStream获取数据的COMStreambuf,并将其用作自定义COMIStream的缓冲区。一切看起来都很好,

c++ - GCC 4.7 istream::tellg() 在到达 EOF 后返回 -1

以下代码适用于gcc4.4。但是gcc4.7会给出断言失败。#include#include#includeusingnamespacestd;intmain(){stringinput("abcdefg");stringstreamiss(input);ostringstreamoss;oss在gcc4.7中,如果istream已经到达EOF,tellg()将返回-1。不会调用pubseekoff()和seekoff()在gcc4.4中这不是问题。应该是gcc4.4还是gcc4.7?为什么? 最佳答案 根据C++11第27.7.2