考虑以下最小示例:
#include <range/v3/all.hpp>
#include <iostream>
namespace rng = ranges::v3;
int main()
{
std::vector<int> v { 6, 2, 3, 4, 5, 6 };
auto f = [](auto a, auto b) { return a*0.3 + b*0.7;};
auto rng = v | rng::view::partial_sum(f);
for(auto i : rng)
{
std::cout<<i<<" ";
}
}
这输出
6 3 2 3 4 5
我本以为会在这里看到双数,但结果显然是整数。这与 view::transform 的行为相反。
这样做的原因是因为在实现中,running-sum 值具有与源范围对应的类型:
semiregular_t<range_value_type_t<Rng>> sum_;
这是故意的还是错误?
讨论:我发现在尝试获取有效返回类型时遇到了麻烦,因为转换函数同时使用源范围和结果范围作为参数并生成返回值类型。下一个应用程序使用源范围类型和此返回类型来生成另一个(可能不同的)返回类型,依此类推。
原则上,通过这种方式,可以重复将源值类型与转换函数的结果类型链接起来。只有当结果类型“收敛”到所有其他中间结果都可以转换为的特定类型时,这种重复迭代才会产生可用的东西(在上面的示例中,这种类型是 double,它已经获得在第一次调用转换函数之后)。
根据这一观察,可以提出一种解决方法:将二进制转换函数应用给定次数,并使用 common_type 作为结果范围的值类型(如果发现收敛,则过早停止).在最简单的情况下,迭代次数仅为一次。如果此迭代没有导致某些合理的事情,仍然可以求助于源值类型(或编译器错误)。
为了清楚起见,这里是上面例子的应用:
First iteration : f(int,int) -> yields "double"
Second iteration: f(int,double) -> yields "double"
Third iteration : f(int,double) -> yields "double"
第三次迭代后模式收敛,因此停止并选择普通类型 double 作为返回范围的 value_type。
我不确定这种方法是否在所有理论情况下都完全有效,但至少它在第一个示例中给出了 double —— 我想这是每个人都强烈期待的。
最佳答案
ranges::view::partial_sum通过设计反射(reflect)了 std::partial_sum 的语义.如果你运行:
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
int main()
{
std::vector<int> v { 6, 2, 3, 4, 5, 6 };
auto f = [](auto a, auto b) { return a*0.3 + b*0.7; };
std::vector<double> rng;
std::partial_sum(v.begin(), v.end(), std::back_inserter(rng), f);
for(auto i : rng)
{
std::cout<<i<<" ";
}
}
你应该得到 exactly the same output as from the program in the OP .与许多 range-v3 View 一样,此 View 的工作是计算与标准算法计算的结果序列相同的结果序列,但这样做是惰性的。
std::partial_sum指定对类型与输入范围的值类型相同的累加器进行操作。 [partial.sum]/2说:
Effects: For a non-empty range, the function creates an accumulator
accwhose type isInputIterator's value type, initializes it with*first, and assigns the result to*result. For every iteratoriin[first + 1, last)in order,accis then modified byacc = acc + *ioracc = binary_op(acc, *i)and the result is assigned to*(result + (i - first)).
要表现相同,ranges::view::partial_sum还使用类型为输入范围的值类型的累加器。
对于 OP,您可以使用 double 获得所需的结果。作为输入范围的类型。使用 range-v3,这很容易通过与 ranges::view::transform(ranges::convert_to<double>{}) 组合来即时完成。 :
#include <range/v3/all.hpp>
#include <iostream>
namespace rng = ranges::v3;
int main()
{
std::vector<int> v { 6, 2, 3, 4, 5, 6 };
auto f = [](auto a, auto b) { return a*0.3 + b*0.7;};
auto rng = v | rng::view::transform(rng::convert_to<double>{}) |
rng::view::partial_sum(f);
for(auto i : rng)
{
std::cout<<i<<" ";
}
}
which produces the desired output :
6 3.2 3.06 3.718 4.6154 5.58462
关于range-v3 partial_sum View 的 C++ 意外值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135838/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t
我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc
我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat
这是我在ChefRecipe中的一blockRuby:#ifdatadirdoesn'texist,moveoverthedefaultoneif!File.exist?("/vol/postgres/data")execute"mv/var/lib/postgresql/9.1/main/vol/postgres/data"end结果是:Executingmv/var/lib/postgresql/9.1/main/vol/postgres/datamv:inter-devicemovefailed:`/var/lib/postgresql/9.1/main'to`/vol/post