草庐IT

ostream_iterator

全部标签

C++ 缩进重载 ostream 运算符

假设Ì有一些类并通过重载左移运算符添加了输出功能:structFoo{inti=1;std::strings="hello";};auto&operator什么是缩进输出的好方法?例子:如果我写std::cout输出是:1hello显然,hello没有缩进。是否有一种简单的方法来缩进整个输出(而不仅仅是第一个元素)? 最佳答案 您正在序列化Foo对象吗?所以从逻辑上讲,Foo的序列化字符串是Foo的实现细节。您可以按照这些思路编写您自己的流类或其他东西,但那是对问题的过度设计。auto&operator

c++ - 编写一个 ostream 过滤器?

我想写一个简单的ostream,它包装一个参数ostream并在将它传递到参数流之前以某种方式更改流。转换很简单,例如更改字母或删除单词从ostream继承的简单类会是什么样子?我应该覆盖哪些方法? 最佳答案 std::ostream不是实现过滤的最佳位置。它没有适当的虚函数来让您执行此操作。您可能想要编写一个派生自std::streambuf的类,其中包含一个包装的std::ostream(或一个包装的std::streambuf),然后使用此std::streambuf创建一个std::ostream。std::streambu

c++ - boost 图形库 : Bundled Properties and iterating across edges

只是想了解一下BoostGraphLibrary,我有几个问题。我正在编写一些代码,它是BGL图的包装类。我的想法是,我可以随心所欲地操作图表,然后调用包装方法以GEXF(XML)格式输出图表。我的代码是这样的:structVertex{std::stringlabel;...};structEdge{std::stringlabel;doubleweight;...};typedefboost::adjacency_listGraphType;templateclassGEXF{private:Graphgraph;...};templatevoidGEXF::buildXML(){

c++ - 具有不完整 Value 参数的 Boost.Iterator Facade

我正在尝试将boost::iterator_facade与不完整的Value一起使用模板参数。这失败了,因为iterator_facade正试图检查类型is_pod。这是预期的行为吗?我可以解决这个限制吗某种方式?我可以编写一个简单地代理foo和为它提供隐式转换,但我宁愿有一个更简单的解决方案。#includeclassiter:publicboost::iterator_facade{private:friendclassboost::iterator_core_access;voidincrement(){}boolequal(iterconst&other)const{retur

c++ - boost::spirit 1.53 multi_pass iterator相关的编译错误

代码:typedefstd::string::const_iteratoriterator;namespaceparsers{namespacespirit=::boost::spirit;namespaceascii=::boost::spirit::ascii;namespacephoenix=::boost::phoenix;spirit::qi::ruleaction_parser='"'>spirit::qi::lit("action")>spirit::qi::labels::_r1>'"';}错误:>1>CL:warning:Thisheaderisdeprecated.

c++ - std::deque: "insertion and deletion of elements may invalidate iterators"是什么意思?

我正在阅读有关std::deque容器的信息,文档指出Insertionanddeletionofelementsinstd::dequemayinvalidateallitsiterators这是我对上述陈述的理解版本,如果我误解了陈述或遗漏了什么,请告诉我考虑以下代码std::deques;s.push_back(12);autoi=s.begin();s.push_front(45);//Afterpushing45atthebacknow`i`maybeinvalid.这个理解正确吗? 最佳答案 你是对的。例如之后std::

c++ - C++11 中的 checked_array_iterator<T>

C++11提供了std::array包装C数组,但仅限于在编译时知道数组大小的地方。处理大小仅在运行时已知的数组的最佳方法是什么?背景我正在将一些代码从MSVC移植到GCC。MSVC提供了stdext::checked_array_iterator为这样的代码行提供一些保护的模板:std::copy(v.begin(),v.end(),stdext::checked_array_iterator(arr,numVals));到目前为止,我可以想到两种选择:放弃安全检查或编写自己的实现。关于这一点,如果您对此实现提出任何建设性意见,我将不胜感激:namespacestdext{templ

通用模板 ostream << 运算符的 C++ 不明确重载

这个问题紧接我之前的问题:Genericoperator我想在哪里实现一个通用的适用于拥有to_str()的任何类的运算符方法。我已经成功地检查了一个类是否实现了to_str()方法与用途std::cout感谢这个answer.但是,我在编写模板时遇到困难ostream运营商制作std::cout有效。以下测试代码:#include#include#includetemplateusingvoid_t=void;templatestructhas_to_string:std::false_type{};templatestructhas_to_string().to_str())>>:

c++ - 如何在没有此运算符的情况下为类型实现默认运算符<<(ostream&, T)?

自std::to_string添加到c++11,我开始实现to_string而不是更传统的operator.我需要将两者链接在一起,以便合并依赖operator的库.我希望能够表达如果T有operator,用它;否则,使用std::to_string.我正在制作一个更有限的版本的原型(prototype),该版本支持operator对于所有枚举类。enumclassMyEnum{A,B,C};//plainversionworks//std::ostream&operator::value>::type>std::ostream&operator编译器说error:nomatchfor

c++ - std::experimental::ostream_joiner 和 std::pair

在c++17/g++7中,终于有了怀念已久的ostream_joiner。它可以正确输出到ostream,使用中缀定界符分隔集合元素。#include#include#include#include#include#includeusingstring=std::string;#if1structpair{stringfirst;stringsecond;};#elseusingpair=std::pair;#endifstd::ostream&operatorpairs={{"foo","bar"},{"baz","42"}};std::copy(std::begin(pairs),