草庐IT

int_types

全部标签

c++ - 我如何将一个 int 添加到一个字符串

我有一个字符串,我需要向它添加一个数字,即一个整数。喜欢:stringnumber1=("dfg");intnumber2=123;number1+=number2;这是我的代码:name=root_enter;//pullnamefromanotherstring.size_tsz;sz=name.size();//findthesizeofthestring.name.resize(sz+5,account);//addtheaccountnumber.cout这行得通……有点,但我只得到“*name*88888”而且……我不知道为什么。我只需要一种方法将int的值添加到字符串的末

c++ - 使用波浪号获取 int 的 MAX 值

我尝试使用代字号获取int的MAX值。但输出不是我所期望的。当我运行这个时:#include#includeintmain(){inta=0;a=~a;printf("\nMaxvalue:%d",-a);printf("\nMaxvalue:%d",INT_MAX);return0;}我得到输出:最大值:1最大值:2147483647我想,(例如)如果我在RAM中有0000(我知道第一位显示的是数字pozitiv或negativ)。在~0000=>1111和-(1111)=>0111之后,我会得到MAX值。 最佳答案 您有一个32

C++:如何将 int 转换为 unsigned long 而不更改任何位?

C++:如何将int转换为unsignedlong且不更改任何位?我想将值打包和解包到内存中。字长为64位。这个片段说明了问题:intv1=-2;//0xfeunsignedlongv2=(unsignedlong)v1;//0xfffe,Iwant0x00fe简单的解决方案是:unsignedlongv2=(unsignedint)v1;//0x00fe但是,这段代码位于目标类型是参数的模板中,所以我不得不求助于此:uint64target=mem[index]&mask;uint64v;if(value假设,例如,“value”的类型是一个int(16位)并且bits=16。目标是

c++ - 如何在 C++03 中使用 "dereference a type"?

如何在C++03中获取另一种类型的“解引用类型”?请注意,它可以是其他可取消引用的类型,例如std::vector::iterator.例如如果我有templatestructMyPointer{Tp;???operator*(){return*p;}};我怎样才能弄清楚用什么替换???和?(没有提升!我想知道如何自己解决。) 最佳答案 templatestructdereference;templatestructdereference{typedeftypenameTtype;};templatestructMyPointer{

c++ - 我可以将 unsigned char* 转换为 unsigned int* 吗?

error:invalidstatic_castfromtype‘unsignedchar*’totype‘uint32_t*{akaunsignedint*}’uint32_t*starti=static_cast(&memory[164]);我分配了一个字符数组,我想将4个字节作为32位int读取,但出现编译器错误。我知道我可以像这样移位:(start[0]它会做同样的事情,但这是很多额外的工作。是否可以通过某种方式将这四个字节转换为int? 最佳答案 static_cast旨在用于“行为良好”的转换,例如double->int

c++ - 错误 : ‘i’ does not name a type with auto

这个问题在这里已经有了答案:HowdoIenableC++11ingcc?(4个答案)关闭7年前。我是C++新手,这是我的程序#include#include#include#include#includeintmain(){staticconstdoublearr[]={16.0,2.2,77.5,29.0,24.0};std::vectorvec(arr,arr+sizeof(arr)/sizeof(arr[0]));std::transform(vec.begin(),vec.end(),vec.begin(),bind2nd(std::minus(),3.0));for(aut

c++ - 如何禁止简单的赋值 MyClass=int?

如果我有一个类具有这样的属性:structMyClass{double**arowofpointers;intcapacity;};现在,如果任务说“确保主函数中的这行代码是合法的:MyClassa(10);//makesavariablewhosetypeisMyClassthathasthecapacityof10但是要确保main函数中的下面这行代码是不合法的:MyClassa=10;不过,main函数中的以下代码行应该是合法的:a=b+c;其中a,b,c均为MyClass类型的变量。我应该制作哪些构造函数?我应该在删除时设置什么吗? 最佳答案

c++ - 通过将其分配给 const std::tuple<int, int>& 来延长 std::tuple<int&,int> 的生命周期

我使用的是std::tuple类并发现我会说的是相当意外的行为。考虑代码:#include#includeinti=20;std::tuplef(){returnstd::tuple(i,0);}intmain(){conststd::tuple&t=f();intj=++i;std::cout(t)这似乎编译并打印了20在所有主要编译器上。由于两种类型不同,此标准是否符合标准或未定义的行为?我知道可以通过将临时分配给constT&来延长它的生命周期。,但据我所知std::tuple与std::tuple的类型不同. 最佳答案 这是

c++ - 从 std::array 等获取 size_type 的惯用方法

由thisquestion触发,我想出了以下代码(在我的回答中是boost::array,但同样适用于std::array):template::size_typesize>voidDataTransform(std::arraydata){}我对::size_type一点都不满意.我必须以一定的大小进行实例化,才能知道size_type.对于std::array我本可以使用size_t,那么一般情况呢?如果size_type怎么办不是size_t?或者更一般的(即不适用于std::array)如果size_type怎么办?不同的尺寸是不同的(愚蠢但可能)?我知道这个问题相当学术,有很

c++ - 标准是否保证 uint8_t、int8_t 和 char 都是唯一类型?

似乎以下内容可以保证通过(已询问here):#includestatic_assert(!std::is_same_v);static_assert(!std::is_same_v);引用cppreference[char]hasthesamerepresentationandalignmentaseithersignedcharorunsignedchar,butisalwaysadistincttype是否也保证int8_t和uint8_t根据显式签名类型定义未定义就char而言,因此也形成一组具有char?的3种不同类型#include#includestatic_assert(