草庐IT

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++ - C++ 中的对是否有 NULL 等价物?

如果我在C++中有一个未分配的对,我想使用什么而不是NULL?例如,假设我有如下(伪)代码:pairbestPair;//GlobalvariableupdateBestPair(vectora,vectorb){bestPair=NULL;for(/*loopthroughaandb*/){if(/*pair(a,b)isbetterthanbestPairandbetterthansomebaseline*/)bestPair=make_pair(a,b);}if(bestPair!=NULL)//Foundanacceptablebestpairfunction(bestPair

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::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++ - 从 ‘void’ 到非标量类型的转换‘std::pair<std::basic_string<char, std::char_traits<char>

我在电子表格obj中有一堆对:std::stack>undoStack;我正在尝试弹出堆栈并将其分配给另一对:std::pairchange=spreadsheets.at(i).undoStack.pop();我收到这个错误:error:conversionfrom‘void’tonon-scalartype‘std::pair,std::allocator>,std::basic_string,std::allocator>>’requested这里出了什么问题? 最佳答案 stack::pop()返回void但您正试图将其分配

c++ - std::make_pair:无法将 'ch'(类型 'char')转换为类型 'char&&'

这个问题在这里已经有了答案:C++11make_pairwithspecifiedtemplateparametersdoesn'tcompile(1个回答)关闭9年前。以下代码有什么问题:#include#include#include#include#includeintmain(){std::vector>vec;for(unsignedi=0;i(ch,number))!=vec.end());std::cout(ch,number));}}它确实可以很好地编译:g++test.cxx但失败了:$g++-std=c++11test.cxx/tmptest.cxx:Infunct

c++ - 使用 pair<int, int> 或 string 作为 map<> 键,哪个更高效?

我想使用map存储键值对。map的key应该包含一个点的坐标(int)信息。一种可能性是转换ints至string.例如,坐标(x,y)可以表示为"x#y"并存储此字符串"x#y"作为key。另一种可能性是使用一对将坐标存储为pair并使用这个pair作为关键。哪种方法更好,为什么? 最佳答案 这取决于您对高效的定义,我们很快就会转向可能被视为过早优化的内容。有很多因素在起作用,顺便说一句,你表达问题的方式我认为我们应该看一个非常简单的问题:您的主要考虑因素可能是:Storage:每个键占用多少内存速度:关键比较的复杂程度初始化:创

C++ 对和指针

我不知道我该怎么做才能在C++中完成这项工作。目的是:pairfoo(){if(cond){returnstd::make_pair(1,2);}returnNULL;//error:noviableconversionfrom'long'to'pair}voidboo(){pairp=foo();if(p==NULL){//error:comparisonbetweenNULLandnon-pointer('int,int'andNULL)//doA}else{inta=p.first;intb=p.second;//doB}}因为我不能在C++中使用returnNULL,这是我的第