草庐IT

from_int

全部标签

c++ - 为什么没有 std::from_string()?

为什么没有templateTstd::from_string(conststd::string&s);在C++标准中?(看看如何有一个std::to_string()函数,我的意思是。)PS-如果您对未被采纳/考虑的原因有任何想法,请回答/评论并说明原因,而不是投反对票。我实际上并没有提议将其包含在标准中。 最佳答案 正如NicolBolas所指出的,to_string从来都不是模板,而只是一组重载函数。制作这样的函数模板并不好,因为一般地做它们可能效率不高,而且只能表现得像字符串流。所以from_string同样不应该是函数模板,

c++ - 指向 deque<int>::push_back 的指针

#include#includeusingnamespacestd;main(){typedefvoid(deque::*func_ptr)(int);func_ptrfptr=&deque::push_back;}我试图获取指向该函数的指针,但出现编译错误error:cannotconvert‘void(std::deque::*)(constvalue_type&){akavoid(std::deque::*)(constint&)}’to‘func_ptr{akavoid(std::deque::*)(int)}’ininitializationfunc_ptrfptr=&deq

C++0x 用基于范围的 for 循环替换 for(int i;;) 范围循环的方法

所以我一直在使用GCC4.6进入新的C++,它现在具有基于范围的for循环。我发现这非常适合迭代数组和vector。主要出于审美原因,我想知道是否有办法用它来代替标准for(inti=min;i用类似的东西for(int&i:std::range(min,max)){}新的C++标准中是否有内置的东西允许我这样做?还是我必须编写自己的范围/迭代器类? 最佳答案 我在任何地方都看不到它。但这将是相当微不足道的:classrange_iterator:publicstd::input_iterator{intx;public:range

通过模板对unsigned int的C++限制

我正在使用一个模板将整数类型转换为二进制值的字符串表示形式。我使用了以下内容:templatestd::stringToBinary(constT&value){conststd::bitset::digits+1>bs(value);conststd::strings(bs.to_string());returns;}它适用于int但不能用unsignedint编译:unsignedintbuffer_u[10];intbuffer_i[10];...ToBinary(buffer_i[1]);//compileandworksToBinary(buffer_u[1]);//does

c++ - 将一行 cv::Mat 转换为 int

我有一个来自FREAK描述提取的描述符矩阵,其中每一行都是一个包含64个元素的描述符。我需要创建一个vector由于系统要求,从这个矩阵。到目前为止我试过这个:Mat_descriptors;std::vectordescriptors;introw;for(inti=0;i这是正确的还是有更好的方法? 最佳答案 descriptors中的所有值将指向带有此代码的堆栈上的变量row。看一个opencvMat的定义,row按值返回://returnsanewmatrixheaderforthespecifiedrowMatrow(in

c++ - 为什么 `constexpr const int &a = 1;` 在 block 范围内失败?

N45277.1.5[dcl.constexpr]p9Aconstexprspecifierusedinanobjectdeclarationdeclarestheobjectasconst.Suchanobjectshallhaveliteraltypeandshallbeinitialized.Ifitisinitializedbyaconstructorcall,thatcallshallbeaconstantexpression(5.20).Otherwise,orifaconstexprspecifierisusedinareferencedeclaration,everyf

c++ - 强制 String to int 函数消耗整个字符串

给定一个应该代表数字的字符串,我想将它放入一个转换函数中,如果整个字符串没有转换,该函数将提供通知。对于输入:“12”:istringstream::operator>>输出12atoi输出12stoi输出12对于输入"1X"我想要一个失败响应,但我得到:istringstream::operator>>输出1atoi输出1stoi输出1对于输入"X2":istringstream::operator>>输出0并设置错误标志atoi输出0stoi抛出错误[LiveExample]有没有办法在输入"1X"时引发错误行为? 最佳答案 编

c++ - 这个技巧是否会使在构造函数 'just work' 中调用 shared_from_this() 变得危险?

C++专家的问题。我们都知道在类构造函数中调用shared_from_this()会导致bad_weak_ptr异常,因为尚未创建实例的shared_ptr。为了解决这个问题,我想到了这个技巧:classMyClass:publicstd::enable_shared_from_this{public:MyClass(){}MyClass(constMyClass&parent){//Createatemporarysharedpointerwithanull-deleter//topreventtheinstancefrombeingdestroyedwhenit//goesouto

c++ - 错误 C2440 : 'initializing' : cannot convert from 'initializer-list'

#includeusingnamespacestd;structTDate{intday,month,year;voidReadfromkb(){cout>day>>month>>year;}voidprint(){cout为什么我收到错误1​​errorC2440:'initializing':cannotconvertfrom'initializer-list'to'TDate'and2IntelliSense:toomanyinitializervalues。当我删除boolvalid和intID时,程序可以运行。为什么会这样? 最佳答案

c++ - 模板参数可以同时是 int 和 unsigned long 吗?

我有以下代码:#includetemplateclassA{};templateclassB;templateclassB>{};intmain(){B>b;return0;}在这里,B被模板化为int而A被模板化为size_t,它是一个unsignedlong我正在使用的两个编译器。当我使用编译器1(当前编译器)时,一切都按照我期望的方式编译和工作。当使用编译器2(我们正在转向的编译器)时,我收到一个编译器错误,指出B没有采用unsignedlong的模板特化-它已解释3作为unsignedlong因为它需要是A的一个,但是对于B。修复很明显,-只需更改B也采用size_t(或更改A