草庐IT

try-convert

全部标签

c++ - 警告 : ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data member

这个问题在这里已经有了答案:constexprconstvsconstexprvariables?(3个回答)关闭3年前。为什么这段代码会返回警告warning:ISOC++forbidsconvertingastringconstantto‘char*’[-Wwrite-strings]如果Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.Aconstexprspecifierusedinafunctionorstaticmembervariab

c++ - try-catch block 是否会降低性能

这个问题在这里已经有了答案:InwhatwaysdoC++exceptionsslowdowncodewhentherearenoexceptionsthown?(6个回答)关闭9年前。这个link国家,Tocatchexceptionswemustplaceaportionofcodeunderexceptioninspection.Thisisdonebyenclosingthatportionofcodeinatryblock.Whenanexceptionalcircumstanceariseswithinthatblock,anexceptionisthrownthattra

c++ - 函数 try block 何时有用?

我想知道程序员何时使用函数tryblock。什么时候有用?voidf(inti)try{if(i输出:(在ideone)greaterthanzerolessthanzero编辑:有些人可能认为函数定义的语法不正确(因为语法看起来不熟悉),我不得不说不,它不是不正确的。它被称为函数尝试block。请参阅C++标准中的§8.4/1[dcl.fct.def]。 最佳答案 您在构造函数中使用它来捕获初始化程序的错误。通常,您不会发现这些错误,因此这是一个非常特殊的用途。否则,它是无用的:除非我被证明是错误的,voidf()try{...}

c++ - 新 (std::nothrow) 与 try/catch block 中的新

我在学习new之后做了一些研究,不像我习惯的malloc(),分配失败时不会返回NULL,发现有两种不同的方式检查new是否成功。这两种方式是:try{ptr=newint[1024];}catch(std::bad_alloc&exc){assert();};和ptr=new(std::nothrow)int[1024];if(ptr==NULL)assert();我相信这两种方法可以实现相同的目标,(当然,如果我错了,请纠正我!),所以我的问题是:这是检查new是否成功的更好选择,完全基于可读性、可维护性和性能,同时忽略事实上的c++编程约定。 最佳答案

c++ - 为什么 Try-Catch block 会影响封闭范围内的变量?

为什么外层的temp在捕捉到第一个异常后会变空?#includeintmain(){std::stringtemp("exception");intvalue;while(std::cin>>value&&value!=0){try{if(value>9)throwtemp;elsestd::cout输入:121113输出:12exception//PrintingEmptystring预期输出:12exceptionexception我用g++7.3.0编译我的代码。 最佳答案 这似乎是GCC的复制省略实现中的一个错误。C++标准

c++ - if (condition) try {...} 在 C++ 中合法吗?

例如:if(true)try{//worksasexpectedwithbothtrueandfalse,butisitlegal?}catch(...){//...}换句话说,将try-block放在if条件之后是否合法? 最佳答案 tryblock(在C++中是statement)的语法是trycompound-statementhandler-sequence而if的语法是:attr(optional)if(condition)statement_trueattr(optional)if(condition)statement

arrays - golang : convert uint32 (or any built-in type) to []byte (to be written in a file)

我正在尝试使用unsafe库在Go中将uint32转换为字节数组(4个字节):h:=(uint32)(((fh.year*100+fh.month)*100+fh.day)*100+fh.h)a:=make([]byte,unsafe.Sizeof(h))copy(a,*(*[]byte)(unsafe.Pointer(&h)))前两行是正确的,但随后在copy调用时出现运行时错误(unexpectedfaultaddress)。下一步是调用Write_,err=fi.Write(a)将4个字节写入文件。我发现了具有类似主题的其他问题,但没有一个具有有效代码的问题。我也知道unsafe

Golang : convert slices into map

在Golang中是否有一种简单/简单的方法可以将slice转换为map?就像在perl中将数组转换为哈希一样,使用%hash=@array之类的简单赋值很容易以上会将数组中的所有元素转换为哈希,键是偶数索引元素,值是数组的奇数索引元素。在我的Go代码中,我有一段字符串,并希望将其转换为map。我想知道是否有Go的库代码可以做到这一点。funcmain(){varelements[]stringvarelementMapmap[string]stringelements=[]string{"abc","def","fgi","adi"}}elementsslice应该被转换成字符串映射,

exception - Go vs. 中的 panic recover try catch 用其他语言

我刚刚阅读了thispost关于Go中的panic/recover,我不清楚这与其他主流语言中的try/catch有何不同。 最佳答案 panic/recover是功能范围的。这就像说每个函数中只允许一个try/catchblock,并且try必须覆盖整个函数。这使得以与java/python/c#等使用异常相同的方式使用Panic/Recover非常烦人。这是故意的。这也鼓励人们以设计使用的方式使用Panic/Recover。您应该从panic()中恢复(),然后将错误值返回给调用者。

go - 格式浮点 : convert float number to string

这个问题在这里已经有了答案:HowtoformatfloatingpointnumbersintoastringusingGo(2个回答)关闭9年前。http://golang.org/pkg/strconv/http://play.golang.org/p/4VNRgW8WoB如何将float转换为字符串格式?这是谷歌Playground,但没有得到预期的输出。(2e+07)我想得到“21312421.213123”packagemainimport"fmt"import"strconv"funcfloattostr(input_numfloat64)string{//toconve