草庐IT

c++ - 通过 make_shared 用 shared_ptr 包装动态数组

我想将一些字节写入数组。为了使用现代C++,我决定使用智能指针。#include#includeusingnamespacestd;voidwriteUint32_t(uint32_tvalue,unsignedchar*p){*p=static_cast((value>>24)&0xFF);*(++p)=static_cast((value>>16)&0xFF);*(++p)=static_cast((value>>8)&0xFF);*(++p)=static_cast((value)&0xFF);}intmain(){autobuf=make_shared(512);uint32_

c++ - 从 char* 缓冲区读取 int32_t 的惯用 cpp14 方法是什么?

给定一个包含int(小端)的字符缓冲区c。如何读作int32_t?我写了这段代码,但感觉不符合cpp的习惯。int32_tv;char*p=(char*)&v;for(inti=0;i 最佳答案 将binary数据从char*缓冲区复制到任何其他数据类型的唯一可移植方法是使用memcpy(或等效字节-copyingmerhodsuchasstd::copy或你自己的模仿这种行为的方法)。memcpy(&my_number,my_buffer,sizeof(my_number));当然,缓冲区应该包含给定数据类型的正确位。如果它源于在

c++ - 从 ‘void*’ 到 ‘unsigned char*’ 的无效转换

我有以下代码;void*buffer=operatornew(100);unsignedchar*etherhead=buffer;我在尝试编译时收到该行的以下错误;error:invalidconversionfrom‘void*’to‘unsignedchar*’为什么我会收到这个错误,我认为void是“无类型”的,所以它可以指向任何东西,或者任何东西都可以指向它? 最佳答案 您需要进行转换,因为如果不先进行转换,您无法将void*转换为任何内容。你需要做的unsignedchar*etherhead=(unsignedchar

c++ - 将 QByteArray 转换为 std::vector<unsigned char>

我尝试转换QByteArray至std::vector使用此代码:unsignedchar*buffer=(unsignedchar*)byteArrayBuffer.constData();std::vector::size_typesize=strlen((constchar*)buffer);std::vectorbufferToCompress(buffer,buffer+size);但是,假设byteArrayBuffer是QByteArray充满了数据,我认为它在线上效果不佳unsignedchar*buffer=(unsignedchar*)byteArrayBuffer

c++ - 无法修改 char* - 内存访问冲突

为什么说“内存访问冲突”?char*str="HelloGuys";intlen=strlen(str);for(inti=0;i 最佳答案 字符串文字存储在内存的只读部分。任何修改字符串文字内容的尝试都会调用未定义行为(大多数实现中的段错误)。而是使用字符数组charstr[]="HelloGuys"; 关于c++-无法修改char*-内存访问冲突,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q

c++ - g++ 字符串 remove_if 错误

代码如下:#include#include#includeusingnamespacestd;intmain(){stringword="";getline(cin,word);word.erase(remove_if(word.begin(),word.end(),isspace),word.end());word.erase(remove_if(word.begin(),word.end(),ispunct),word.end());word.erase(remove_if(word.begin(),word.end(),isdigit),word.end());}在VS2010中

c++ - 15和015有什么区别?

这个问题在这里已经有了答案:WhatdoesitmeanwhenanumericconstantinC/C++isprefixedwitha0?(7个答案)关闭9年前。起初这似乎是一个愚蠢/微不足道的问题,但当我这样做时:charf_gear=15;我得到了正常的输出"☼"但是当我声明它时用零填充它时:charf_gear=015;我得到奇怪的输出,使文本看起来乱码(在一行中)并使前一行空白。当我尝试查看单个角色本身时,我得到以下信息:"  ◘◘@╧S☻ "有什么本质上的不同?15==015不是吗?==编辑==当我发布问题时,StackOverflow更改了文本。我真正看到的输出是几个

c++ - 将无符号字符数组转换为 float 组

在C++中将unsignedchar数组转换为float数组的最佳方法是什么?我目前有一个for循环如下for(i=0;i我还需要反转程序,即从unsignedchar转换为float(float到8bit转换)for(i=0;i如有任何建议,我们将不胜感激谢谢 最佳答案 我认为最好的方法是使用函数对象:template//TmodelsAnystructstatic_cast_func{template//T1modelstypestaticallyconvertibletoTToperator()(constT1&x)const

c++ - 如果图像文件的内容在 char 数组中,如何使用 cv::imdecode?

我在缓冲区jpegBuffer中有一个jpeg图像。我试图将它传递给cv::imdecode函数:MatmatrixJprg=imdecode(Mat(jpegBuffer),1);我收到这个错误:/home/richard/Desktop/richard/client/src/main.cc:108:error:nomatchingfunctionforcallto‘cv::Mat::Mat(char*&)’这是我填充jpegBuffer的方式:FILE*pFile;longlSize;char*jpegBuffer;pFile=fopen("img.jpg","rb");if(pF

c++ - 为什么 strrchr() 返回 `char*` 而不是 `const char*` ?

函数char*strrchr(constchar*str,intch)返回char*内的指针(str)(constchar*)最后一次出现ch的地方位于。所以我们可以写出下面的代码而无需任何转换:#includeintmain(){constcharCONSTSTR[]="foo/bar/foobar.txt";char*ptr=strrchr(CONSTSTR,'/');*ptr++='B';*ptr++='A';*ptr++='D';}返回有什么好处char*而不是constchar*?编辑:作为ShafikYaghmour指出,Howdoesstrchrimplementatio