草庐IT

pair_sum_even_count

全部标签

C++ count_if 函数 - 无法推断模板

我正在尝试使用C++的count_if函数来查看std::string中有多少个十六进制数字。当我尝试以下操作时:strings="123abc";cout我收到以下错误:count.cpp:14:13:error:nomatchingfunctionforcallto'count_if'cout但是,当我使用::isxdigit时,程序会编译并运行。我知道在::之前添加与在全局范围内使用isxdigit有关,但我不确定为什么它在这种情况下有帮助。我也认为它与C++语言环境有关,但我对它们不太熟悉。谢谢! 最佳答案 有一个函数int

c++ - 初始化 std::pair<double, std::array<std::pair<double, double>, 3>>

谁能建议以下代码中std::vector::push_back调用中std::make_pair调用的正确语法:#include#include#includeintmain(){typedefstd::pairPairType;std::vector>>myVector;doubleKey=0.0;PairTypePair1=std::make_pair(1.0,2.0);PairTypePair2=std::make_pair(3.0,4.0);PairTypePair3=std::make_pair(5.0,6.0);myVector.push_back(std::make_pa

c++ - vector<pair<int,int>> 上界

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。我正在尝试使用upper_bound在vector>上,像这样:vector>data;autoup=upper_bound(data.begin(),data.end(),0);VS2012给我以下错误:errorC2784:'boolstd::operator&,conststd::vector&)':couldnotdeducetemplatearg

c++ - pair<int, int> vector 的哈希函数

我正在尝试为vector>实现一个unordered_map。自there'snosuchdefaulthashfunction,我试着想象一个我自己的功能:structObjectHasher{std::size_toperator()(constObject&k)const{std::stringh_string("");for(autoi=k.vec.begin();i!=k.vec.end();++i){h_string.push_back(97+i->first);h_string.push_back(47);//'-'h_string.push_back(97+i->sec

c++ - 如何使用 make_pair 创建一对 id 和 struct(对象)?

我试图像这样创建一对id和对象:#include#include#includestructnum{doublex;doubley;};intmain(){autotmp=std::make_pair(1,{1.0,2.0});}我收到错误error:nomatchingfunctionforcallto'make_pair(int,)'是否有正确的方法来创建一对id和object? 最佳答案 不,这是你应该如何创建你的对:autotmp=std::make_pair(1,num{1.0,2.0});或者(如@StoryTeller

c++ - MSVC std::pair 实现:SFINAE 是否在此处正确应用?

考虑以下std::pair的代码MicrosoftVisualStudio15.4.5附带的STL实现的默认构造函数:template::value&&is_default_constructible::value>>constexprpair():first(),second(){//defaultconstruct}我设置了/std:c++latest选项,所以,根据标准(我在这里使用草案n4659)我希望如果_Ty1中的任何一个,这个构造函数将被排除在重载决议之外。或_Ty1不是默认可构造的:23.4.2Classtemplatepair[pairs.pair]EXPLICITc

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct

c++ - 为什么 std::pair 在赋值中调用显式构造函数

考虑以下代码:#include#includestructBase{intbaseint;};structDer1:Base{intder1int;Der1():der1int(1){}explicitDer1(constBase&a):Base(a),der1int(1){std::cerrstructMyPair{Tfirst;Usecond;};intmain(){Der1d1;Der2d2;std::pairp1;std::pairp2;p1=p2;//ThiscompilessuccessfullyMyPairmp1;MyPairmp2;mp1=mp2;//Thiswillr

C++ 将 pair<int,int> 的 vector 复制到 vector<int>

我有一个vector对,我需要将它们线性复制到一个整数vector。我有以下运行良好的代码,但考虑到C++中的结构填充问题,我不确定它是否安全。std::vector>test_vector;for(inti=0;iint_vec(test_vector.size()*2);std::copy(reinterpret_cast(&(*test_vector.begin())),reinterpret_cast(&(*test_vector.end())),int_vec.begin());现在,我的问题是-上面的代码安全吗?如果没有,是否有一种无需编写循环即可实现的优雅方法?

c++ - std::make_pair 类型推导

我遇到了一些奇怪的事情,我想得到解释。以下代码片段提供了一个简单的类模板type和两个operators:一个用于type的特化和一个std::pair的type特化。#include#includetemplateclasstype{public:Tvalue_;};templatestd::basic_ostream&operator&os,typeconst&a){returnosstd::basic_ostream&operator&os,std::pairconst&a){returnosintmain(){usingfloat_type=type;float_typecon