草庐IT

pair_sum_even_count

全部标签

c++ - 为什么 boost::assign::list_of 不适用于 pair<string, vector<string>>?

我不明白为什么这不起作用(VisualC++2012):#include#include#include#includeusingnamespacestd;intmain(){pair>("^",boost::assign::list_of("rules"));}错误是:include\utility(138):errorC2668:'std::vector::vector':ambiguouscalltooverloadedfunctionwith[_Ty=std::string]include\vector(786):couldbe'std::vector::vector(std:

c++ - 如何创建包含 std::atomic 的 std::pair?

我不知道如何创建以下内容:std::pair,int>我总是得到/usr/include/c++/5.5.0/bits/stl_pair.h:139:45:error:useofdeletedfunction'std::atomic::atomic(conststd::atomic&)':first(__x),second(std::forward(__y)){}我试过了std::pair,int>pair=std::make_pair(true,1);//doesn'tworkstd::pair,int>pair=std::make_pair({true},1);//doesn'tw

c++ - binary_search 与 std::pair 使用自定义运算符

我正在尝试进行binary_search,包括一个整数对vector和一个整数,如下所示:#include#includeusingnamespacestd;typedefvector>int_pairs;booloperator&r){returnr.first(1,2));pairs_vec.push_back(pair(2,2));size_ti(2);binary_search(pairs_vec.begin(),pairs_vec.end(),i);}编译器告诉我operator未定义:erreur:nomatchfor‘operator’)我的做法是否正确?我尝试以多种不同

c++ - 不完整类型 struct std::hash 与 unordered_map 的无效使用,其中 std::pair of enum class 作为键

我想使用unordered_map,std::uint8_t>用于管理一些像素图格式。这里是最少的代码:#include#include#include#include#includeenumclassPNM:std::uint8_t{PBM,PGM,PPM};enumclassFormat:bool{BIN,ASCII};structpair_hash{public:templatestd::size_toperator()(conststd::pair&x)const{returnstd::hash()(x.first)^std::hash()(x.second);}};intma

c++ - 为什么 std::map 有一个名为 count 的成员函数?

这个问题在这里已经有了答案:WhydoesSTLsethavecount()whenallelementsaresupposedtobeunique?(1个回答)关闭4年前。我正在学习C++,很明显,一种检查std::map中是否存在特定键的方法是使用成员函数count。但我的第一个想法是:键不应该是唯一的吗?并检查documentation实际上它们是唯一的,因此count将返回0或1。把它叫做count是不是有点违反直觉?为什么不存在?对我来说,在您期望元素出现多次的列表中计数是有意义的,但如果该方法只允许返回1或0,那对我来说就没有意义。我错过了什么吗?是否有理由将其称为coun

c++ - 为什么 std::make_pair 按值而不是 const 引用获取输入?

引用本网站http://www.cplusplus.com/reference/std/utility/make_pair/std::make_pair具有此签名(和可能的实现):templatepairmake_pair(T1x,T2y){return(pair(x,y));}我想知道为什么std::make_pair的输入参数是按值而不是常量引用?这有什么特别的原因吗? 最佳答案 它最初是通过const引用获取参数,但这引入了一些意想不到的问题。缺陷报告后改为按值传递:http://www.open-std.org/jtc1/s

c++ - std::count 的复杂性

我在一些在线代码测验网站上有一个复杂性限制,即代码在时间和内存上都不应超过O(N),其中N是vectorA的大小。我的代码完全是(完整代码):intfoo(intX,conststd::vector&A){autoN=A.size();autototal_hit=std::count(A.cbegin(),A.cend(),X);autoK=N-total_hit;if(K=N){return-1;}returnK;}我得到了超过时间复杂度的结果。有没有可能而不是他们错了? 最佳答案 根据ref:Complexity:exactly

c++ - std::pair<U,V> 对齐控制

我注意到在尝试将std::pair保存到二进制文件中时发生了一件令人不快的事情:std::pair与单词对齐。它在处理器效率方面可能很有用,但需要更多存储空间,所以我想将std::pair的对齐模式切换为1字节。我的编译器是MSVC++2012。#includeintmain(){structS_a{doublea;size_tb;};#pragmapack(1)structS_wa{doublea;size_tb;};std::cout)))我试过了,但是没用:#pragmapack(1)typedefstd::pairQ;std::cout 最佳答案

c++ - std::pair 提示类型不完整

如何编译下面的代码?#include#includestructA;templatestructB{T*p;B&operator=(B&&);B&operator=(T&&);};intmain(){//typedefBtype;//finetypedefB>type;//errornoexcept(std::declval()=std::declval());return0;}PS:TypeB模拟了boost::recursive_wrapper,由于同样的原因编译失败。 最佳答案 typedef本身不是问题。写structfoo

c++ - 返回类型足够大以保存结果的 Sum 函数

这是C++Primer第16.2.3章(问题16.41)中的一个问题:Writeaversionofsumwithareturntypethatisguaranteedtobelargeenoughtoholdtheresultoftheaddition.我确信可能有一些相当晦涩的STL函数可以完成这项工作,但在本章的上下文中它介绍了标准类型转换模板,例如remove_reference和make_signed我确定它打算让我结合尾随返回类型来完成此操作。我能做的最好的是:templateautosum(Itfirst,Itsecond)->typenamemake_unsigned: