草庐IT

ranged-loops

全部标签

c++ - 为什么 ranges::ostream_iterator 是默认可构造的?

这个问题遵循评论中的讨论here.在EricNiebler的ranges-v3library中(这有点成为C++20标准的一部分),ranges::ostream_iterator是default-constructible-没有ostream。怎么会?我认为后来有效构造的“虚拟”构造是C++中的反模式,我们正在逐渐摆脱这种缺陷。std::ostream迭代器canonlybeconstructedwithastream(目前-在C++20之前)。似乎我们可以用默认构造的range::ostream_iterator做任何事情...所以,这是怎么回事? 最佳

c++ - 编程 : Principles and Practice Using C++ chapter 4 drill step 6 : General question about numeric range

我想提示用户输入一些double值,然后存储最小值和最大值,然后打印文本。这是我到目前为止的代码:#include#include#include#includeusingnamespacestd;intmain(){doublemin=1000000000;//Hereismyissue!doublemax=-100000000;//Hereismyissue!for(doubleinput;cin>>input;){if(input=='|')return0;elseif(inputmax){max=input;cout所以我的代码工作正常并且做我想做的,但我对处理双最小值和最大值

C++11 使用带有自定义比较函数的 std::equal_range

考虑这个例子(请注意,这只是我为了说明问题而编造的东西。我很清楚有更有效的方法来解析算术表达式,虽然这个主题很吸引人,但这与我的实际无关问题。这只是一个半现实的例子,如果我可以这样说的话。我同意解析器可能会使问题看起来更复杂,但我想不出更抽象的例子)。假设您想做一个简单的表达式解析器。您将从分词器中获取一些字符串,其中一些可能不明确。例如,字符串“-”可以表示一元减号或二进制减号。假设您想获得字符串“-”的所有可能含义。你可以这样做:1)定义一个描述所有可能运算符的排序数组//typesofoperatorsenumclassopType:char{unary,lasso,rasso,

c++ - 为什么 for-loop 不是编译时表达式并且扩展的 constexpr 允许在 constexpr 函数中进行循环

我是这样写代码的#includeusingnamespacestd;constexprintgetsum(intto){ints=0;for(inti=0;i我知道它之所以有效是因为extendedconstexpr.然而在这个问题why-isnt-a-for-loop-a-compile-time-expression,作者给出了他的代码如下:#include#include#includeconstexprautomultiple_return_values(){returnstd::make_tuple(3,3.14,"pi");}templateconstexprvoidfoo

c++ - 为什么我无法在 range-v3 中获取范围的大小?

我想获取姓名以“T”开头的人数:#include#include#includeusingnamespaceranges;intmain(){constautonames=std::vector{"Tony","Peter"};std::cout但是我遇到了巨大的编译错误:λclang-std=c++14test.cpptest.cpp:11:18:error:nomatchingfunctionforcalltoobjectoftype'constranges::v3::adl_size_detail::size_fn'std::cout,std::allocator>>>>,std

c++ - 性能差异:std::accumulate vs std::inner_product vs Loop

今天,我想分享一些在尝试实现这个简单操作时让我大吃一惊的事情:我发现了执行相同操作的不同方法:通过使用std::inner_product。实现谓词并使用std::accumulate函数。使用C风格的循环。我想通过使用QuickBench并启用所有优化来执行一些基准测试。首先,我比较了两个具有浮点值的C++替代方案。这是通过使用std::accumulate使用的代码:constautopredicate=[](constdoubleprevious,constdoublecurrent){returnprevious+current*current;};constautoresul

c++ - 为什么 gcc-4.9.2 不支持 std::string.insert(iterator, range) 返回迭代器

根据cppreference,C++11应该支持:templateiteratorinsert(const_iteratorpos,InputItfirst,InputItlast);但是当我尝试使用g++4.9.2编译以下代码时:std::stringstr{"helloworld"},addition{"hmy"};autoiter=str.erase(str.begin(),str.begin()+4);iter=str.insert(next(iter),addition.begin(),addition.end());//Error我收到以下错误(liveexample):e

c++ - 类 std::out_of_range 在哪里完全定义?

我正在我的UbuntuLinux上的/usr/include/c++下搜索。在/usr/include/c++/stdexcept中,我发现了这个:classout_of_range:publiclogic_error{public:explicitout_of_range(conststring&__arg);};但是我在任何地方都找不到out_of_range()构造函数的定义。此外,当STL抛出out_of_range()异常时,它使用(示例取自STL_vector.h):__throw_out_of_range(__N("vector::_M_range_check"));而且

c++ - libpng 错误 : PNG unsigned integer out of range

当尝试从内存中读取PNG时,我遇到了这个奇怪的错误:libpngerror::PNGunsignedintegeroutofrange这个错误是由引起的png_read_info(png_ptr,info_ptr);它使用以下处理程序:staticvoidReadDataFromBuffer(png_structppng_ptr,png_bytepoutBytes,png_size_tbyteCountToRead){PNGDataPtrdataptr=(PNGDataPtr)png_get_io_ptr(png_ptr);png_uint_32i;coutlenpdataptr->l

c++ - 给定范围内的完美正方形 : abnormal execution of loops

程序编号1:在给定的a和b范围内,其中ab,我想找出一个数字是否是一个完美正方形,如果是,则打印其根。因此,我编写了如下代码:#include#include#include#includeusingnamespacestd;floatsquaredroot(intn){floatlow=0.0,mid;floathigh=(float)n+1;while((high-low)>0.00001){mid=(low+high)/2;if(mid*mid>a>>b;floatroo=0.0;for(i=a;i对于给定的输入15,输出应该是2。但是,上面的程序没有打印任何值。然而,当我尝试使