草庐IT

pair_sum_even_count

全部标签

c++ - 为什么 std::count_if 返回有符号值而不是无符号值?

这个问题在这里已经有了答案:WhydoestheC++standardalgorithm"count"returnadifference_typeinsteadofsize_t?(7个答案)关闭7年前。刚刚意识到std::count_ifreturnsasignedvalue.为什么要这样设计?在我看来,这是没有意义的(结果只能是自然数,即非负整数),因为它不允许做一些简单的事情,比如将这个结果与容器的size()没有得到警告或使用显式类型转换。我真的认为返回类型应该有size_type。我错过了什么吗?

c++ - 如果 count() 是 constexpr 函数,为什么 std::array<int, count()> 不能编译?

这个问题在这里已经有了答案:constexprnotworkingifthefunctionisdeclaredinsideclassscope(3个回答)3年前关闭。为什么下面的C++代码不能用VC2017编译?structFixedMatchResults{staticconstexprstd::size_tcount(){return20;};std::arrayresults;};错误是:errorC2975:'_Size':invalidtemplateargumentfor'std::array',expectedcompile-timeconstantexpression

c++ - std::bind std::shared_ptr 参数不会增加 use_count

以下代码:#include#include#includestructFoo{Foo():m_p(std::make_shared()){}Foo(constFoo&foo){printf("copy\n");}std::shared_ptrm_p;};voidfunc(Foofoo){}intmain(){Foofoo;std::functionf=std::bind(func,foo);printf("usecount:%ld\n",foo.m_p.use_count());f();}得到结果:copycopyusecount:1copy由于复制了Foo,所以我认为m_p的use_

c++ - std::move 与 std::make_pair

有什么区别:std::map>m;Tt1,t2;m.emplace(1,std::make_pair(t1,t2));和:std::map>m;Tt1,t2;m.emplace(1,std::move(std::make_pair(t1,t2)));std::move在这里是多余的吗?std::map::emplace和perfectforwarding是否负责直接在std::中分配std::pairmap? 最佳答案 std::make_pair(...)和std::move(std::make_pair(...))都是右值表达式

c++ - 如何将已排序的 std::list of std::pair 转换为 std::map

我有一个std::list>,我知道这是根据std::stringelement排序的.因为我想做很多std::find_if基于std::string元素,我相信一个std::map与lower_bound和upper_bound会更合适。事实是我想insertstd::map中的元素以一种有效的方式。所以我想使用一个额外的迭代器来制作insert更快。我相信最简单的方法是使用const_reverse_iterator通过std::list并使用begin()的std::map.你会这样做吗,还是一个坏主意?谢谢! 最佳答案 如

C++ std::pair, std::vector 和 memcpy

从a的第一个元素的内存地址memcopymyvect.size()*sizeof(foo)字节是否安全std::vector>myvect放入一个数组structfoo{T1first;T2second;}如果数组分配的元素数量与vector的大小相同?谢谢 最佳答案 不,一个包含T1的类和T2不保证与std::pair相同的布局或对齐方式,至少在C++98中是这样(因为std::pair不是POD类型)。在C++0x中情况可能有所不同。 关于C++std::pair,std::vect

c++ - 如何在另一个 std::pair 中插入一对 std::pair?

我正在声明一个字符串映射到一对对,如下所示:std::map,std::pair>>reference;我将其初始化为:reference.insert(L"First",std::pair,std::pair>(std::pair(-1,-1),std::pair(0,0)));但是,VisualC++给我错误“C2664,没有构造函数可以采用源类型,或者构造函数重载解析不明确”。我是使用模板和STL的新手,我不知道我做错了什么。 最佳答案 >>>无法正确解析(除非你有C++0x编译器)。更改为>>>这个:reference.in

c++ - make_pair 命名空间污染

在我最近编写的代码中,我注意到一个奇怪的行为。当我使用第一个参数为std::pair的make_pair时,make_pair变得“神奇地”在命名空间中可用(我不必使用std::限定符)#includeintmain(){inti1=2;inti2=10;inti3=0;//constructingapairusingstd::make_pair,everything'sokaystd::pairkey=std::make_pair(i1,i2);//here,whyismake_pairsuddenlymagicallyavailablewithoutthe//std::namesp

c++ - glibcxx STL 在其 std::valarray::sum() 的实现中是否不正确?

我在玩弄valarrays当我碰到某些东西时,我认为是编译器的STL实现中的错误。这是我可以制作的最小示例:#include#include#include#include#includeusingnamespacestd;intmain(){valarrayY(0xf00d,1);valarray>X(Y,1);cout这将输出:$g++-std=c++17-O2-Wall-pedantic-pthreadmain.cpp&&./a.outY[0]=f00dX[0][0]=f00dX[0].size()=1X.sum().size()=0您可以在coliru编译并运行它为什么我认为这

ActiverEcord Collection Count Count COMINED FIELD订购时给出mysql2 ::错误:“顺序子句”中的未知列

试图拨打ActivereCord::关系集合的算法正常,除非您将其加入如下:users=User.joins(:foos).select(['users.idasid','users.nameasname','sum(b.blah)asblah','max(foos.baz)asbazness']).joins('leftjointabley_thingsbonusers.id=b.user_id').group('users.id')users.count#noproblemusers.order('nameDESC').count#noproblemusers.order('bazness