草庐IT

range-init

全部标签

c++ - Boost中irange和counting_range的区别

irange之间有什么区别?和counting_range?我需要irange来快速生成一系列整数,如下所示:autoexample=boost::irange(0,5);///resultis{0,1,2,3,4}但是注意到某处的一个示例(链接丢失),该示例使用counting_range来完成相同的任务。对这两者的区别有简单的解释吗? 最佳答案 主要区别在于irange是一个随机访问范围,而counting_range不是。counting_range基于Boost.Iterator的counting_iterator它直接使用

c++ - Boost::Range 中的 itertools.tee 等效项?

Python的itertools有tee用于n-plicatingiterables:deftee(iterable,n=2):it=iter(iterable)deques=[collections.deque()foriinrange(n)]defgen(mydeque):whileTrue:ifnotmydeque:#whenthelocaldequeisemptynewval=next(it)#fetchanewvalueandfordindeques:#loadittoallthedequesd.append(newval)yieldmydeque.popleft()retu

c++ - 使用 boost::range 在 C++ 中进行花式索引

我想使用boost::range来实现类似于NumPy和Matlab中可用的“花式索引”的功能。具体来说,我想使用另一个容器的元素作为索引来选择一个可索引容器的某些元素。例如,可以在Python中执行以下操作:>>>squares=numpy.arange(10)**2#Step1-setupsquares>>>indices=numpy.array([1,3,4])#Step2-setupindices>>>squares[indices]#Step3-fancyindexingarray([1,9,16])在C++中,使用boost::range,我认为上面的代码看起来像这样:#i

c++ - boost 测试不 init_unit_test_suite

我运行这段代码#defineBOOST_TEST_MAIN#defineBOOST_TEST_DYN_LINK#include#include#include#includeusingnamespaceboost::unit_test;usingnamespacestd;voidTestFoo(){BOOST_CHECK(0==0);}test_suite*init_unit_test_suite(intargc,char*argv[]){std::coutadd(BOOST_TEST_CASE(&TestFoo));returnmaster_test_suite;}但是在运行时它说T

c++ - 我可以将 std::begin 和 std::end 专门化为 equal_range() 的返回值吗?

header提供std::equal_range(),以及一些将它作为成员函数的容器。这个函数让我困扰的是它返回一对迭代器,这使得从开始迭代器到结束迭代器的迭代变得乏味。我希望能够使用std::begin()和std::end()这样我就可以使用C++11基于范围的for循环。现在,我听到了关于特化的矛盾信息std::begin()和std::end()-有人告诉我,向std命名空间添加任何内容都会导致未定义的行为,而我也被告知您可以提供自己的std::begin()特化。和std::end().这就是我现在正在做的:namespacestd{template::iterator_ca

c++ - 在 for range 循环中迭代包含 vector 的取消引用的 unique_ptr

为什么这段代码不像我想象的那样工作?for(autoit:*std::make_unique>(std::vector({1,2,3,4,5})))std::coutvector对象在执行循环的第一次迭代之前被销毁 最佳答案 range-basedforloop相当于:{init-statementauto&&__range=range_expression;...}对于您的range_expression,它将是auto&&__range=*std::make_unique>(std::vector({1,2,3,4,5}));但

c++ - 使用 Range v3 范围,如何将 View 和操作组合到一个管道中?

我正在学习C++20范围(使用Range-V3-VS2015)。我有这段代码可以正常工作:stringclean;autotmp1=input|view::remove_if(not_alpha)|view::transform(::tolower);std::copy(tmp1.begin(),tmp1.end(),std::back_inserter(clean));autotmp2=clean|=action::sort|action::unique;但是,我想将定义tmp1和tmp2的两个管道组合成一个管道。那可能吗?我尝试了很多方法,包括在中间添加view::move和vie

c++ - 全局构造函数调用不在 .init_array 部分

我正在尝试在嵌入式目标(ARMCortex-M3)上添加全局构造函数支持。假设我有以下代码:classfoobar{inti;public:foobar(){i=100;}voidinc(){i++;}};foobarfoo;intmain(){foo.inc();for(;;);}我是这样编译的:arm-none-eabi-g++-O0-gdwarf-2-mcpu=cortex-m3-mthumb-cfoo.cpp-ofoo.o当我使用objdump查看.init_array部分时,它显示.init_section的大小为零。我确实得到了一个名为_Z41__static_initia

c++ - 返回条件 `range_expression`

根据某些条件迭代多个已知范围之一的最有效方法是什么?二进制条件的伪代码:forelementin(condition?range_a:range_b)//dowork这个“示例”显示了我使用range-basedforloop的意图但作为std::initializer_list具有引用语义,它将不起作用。constexprautosome_range(boolc)->std::initializer_list{if(c){return{1,2};}else{return{3,4,5};}}boolcond=true;//falsefor(autox:some_range(cond))

c++ - C++11 委托(delegate)的 ctors 是否比调用 init 函数的 C++03 ctors 表现更差?

[这个问题已经过高度编辑;请原谅,我已将编辑内容移至下面的答案中]来自Wikipedia(subarticleincluded)在C++11上:This[newdelegatingconstructorsfeature]comeswithacaveat:C++03considersanobjecttobeconstructedwhenitsconstructorfinishesexecuting,butC++11considersanobjectconstructedonceanyconstructorfinishesexecution.Sincemultipleconstructor