草庐IT

format-conversion

全部标签

c++ - 编写 c++ 函数 format_string 进行格式化,如 std::string 的 sprintf

为了方便使用,我想编写类似于sprintf的格式化函数,只返回std::string,如下所示:std::stringformat_string(constchar*format,...)我可以在那里使用vsnprintf但有问题-我事先不知道临时缓冲区应该有多长。Microsoft上有函数_vscprintf可以做到这一点,但我认为它不可移植?一个选项是让临时缓冲区开始一些已知的大小,然后如果发现它不够用vsnprintf增加它。有更好的方法吗?谢谢附言请在没有提升的情况下给出答案。我知道Boost,但我很好奇如何在没有Boost的情况下实现它。 最佳答案

c++ - 错误 : invalid conversion from 'void (*)()' to 'void (*)()' -- what?

我正在尝试将回调函数从C++传递到OpenGL(CAPI):gluQuadricCallback(qobj,GLU_ERROR,errorCallback);其中errorCallback是编译为C++代码的文件中的函数,声明为voiderrorCallback();代码在Linux上使用g++4.4编译干净,但在Windows上使用mingw32g++4.4时出现以下错误:..\glwidget.cpp:172:error:invalidconversionfrom'void(*)()'to'void(*)()'..\glwidget.cpp:172:error:initializi

c++ - 错误 C2678 : binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion)

我正在尝试对每个元素中包含一个int和一个字符串的vector进行排序。它是一个称为vector食谱的类类型的vector。出现上述错误,这是我的代码:在我的Recipe.h文件中structRecipe{public:stringget_cname()const{returnchef_name;}private:intrecipe_id;stringchef_name;在我的Menu.cpp文件中voidMenu::show()const{sort(recipes.begin(),recipes.end(),Sort_by_cname());}在我的Menu.h文件中#include

c++ - Qt - 获取 "warning: format not a string literal and no format arguments"

在这样的行上不断收到警告qDebug("Anerroroccuredwhiletryingtocreatefolder"+workdir.toAscii());workdir是QString()warning:formatnotastringliteralandnoformatarguments 最佳答案 大概应该是:qDebug("Anerroroccuredwhiletryingtocreatefolder%s",workdir.constData());自qDebug将constchar*作为第一个参数。

c++ - SIMD : Why is the SSE RGB to YUV color conversion about the same speed as the c++ implementation?

我刚刚尝试优化RGB到YUV420转换器。使用查找表可以提高速度,就像使用定点算法一样。然而,我期待使用SSE指令获得真正的yield。我的第一次尝试导致代码变慢,并且在链接所有操作之后,它的速度与原始代码大致相同。我的实现是否有问题,或者SSE说明是否不适合手头的任务?部分原始代码如下:#defineRRGB24YUVCI2_000.299#defineRRGB24YUVCI2_010.587#defineRRGB24YUVCI2_020.114#defineRRGB24YUVCI2_10-0.147#defineRRGB24YUVCI2_11-0.289#defineRRGB24Y

c++ - 为什么隐含的 "lambda to function pointer conversion"禁止静态成员的 "by reference"捕获?

C++11标准说(或者至少,我拥有的版本——不是最终版本):Theclosuretypeforalambda-expressionwithnolambda-capturehasapublicnon-virtualnon-explicitconstconversionfunctiontopointertofunctionhavingthesameparameterandreturntypesastheclosuretype’sfunctioncalloperator.我理解为什么无法从有状态lambda中获取函数指针,因为函数指针本身不能保存任何数据。但是当捕获的对象只是一个静态成员/静

c++ - 有没有办法强制使用 "this->"用于 clang-format/clang-tidy 中的类成员/方法?

我到处搜索,但我可能用错了术语。我还没有为此找到选项。我唯一发现的是这个未回答的问题(但是有点宽泛):CPPlint:Canyouenforceuseofthisforclassvariables?. 最佳答案 鉴于existingoptions,我不相信这在clang-format中是可能的,future也不会。主要原因是程序的工作方式。它不会将C++代码解析为AST,而是将文本标记化而不需要包含(定义它的成员和全局变量)而不是编译数据库(影响定义、包含路径……)它是甚至可以给它一段代码并重新格式化。从问题的性质来看,如果它可以存

c++ - 自定义支持 __attribute__((format))

GCC和Clang都支持对可变参数函数(如printf)进行编译时检查。这些编译器接受如下语法:externvoiddprintf(intdlevel,constchar*format,...)__attribute__((format(printf,2,3)));/*2=format3=params*/在OSX上,Cocoa框架还使用了NSString的扩展:#defineNS_FORMAT_FUNCTION(F,A)__attribute__((format(__NSString__,F,A)))在我们公司,我们有一个自定义的C++框架,其中包含一堆类,例如BaseString,它

c++ - 如何修复 gcc -Wall "embedded '\0' in format"警告

这可能不是很重要,但我正在尝试修复g++提示的所有警告。在下面的代码中,我收到了snprintf()行的“embedded'\0'informat”警告。我该如何解决这个问题?intfilePathSize=path.size()+s.size()+1;charfilePath[filePathSize];snprintf(filePath,filePathSize,"%s%s\0",path.c_str(),s.c_str());提前致谢... 最佳答案 警告是有充分理由的:snprintf将认为\0标记字符串的结尾。如果您确实需

c++ - 如何使用 boost::format 生成十六进制输出?

考虑以下几点:#include#include#include#include#include#include#includeusingnamespacestd;typedefunsignedcharbyte;typedefvectorbyte_array;constbyte_arraybytes=list_of(0x05)(0x04)(0xAA)(0x0F)(0x0D);intmain(){conststringformatter="%1%-%2%-%3%-%4%-%5%";conststringresult=(format(formatter)%bytes[0]%bytes[1]%