草庐IT

c++ - size_type 和 int 之间的区别

#include#includeusingnamespacestd;intmain(){vectorstudent_marks(20);for(vector::size_typei=0;i>student_marks[i];}return0;}我在某处读到,最好使用size_type代替int。它真的会对实现产生巨大影响吗?使用size_type有什么好处? 最佳答案 vector::size_type保证涵盖vector大小的所有可能值范围.一个int不是。请注意vector::size_type通常与std::size_t相同,

c++ - 如何在不使用 to_string 或 stoi 的情况下将 int 转换为 C++11 中的字符串?

我知道这听起来很愚蠢,但我在Windows7上使用MinGW32,并且“to_string未在此范围内声明。”It'sanactualGCCBug,我关注了theseinstructions他们没有工作。那么,如何在不使用to_string或stoi的情况下将int转换为C++11中的字符串?(另外,我启用了-std=c++11标志)。 最佳答案 这不是最快的方法,但您可以这样做:#include#include#includetemplatestd::stringstringulate(ValueTypev){std::ostri

c++ - 填充作为成员变量的 int 数组

我正在使用C++为游戏创建瓦片map。我的问题是,我想在Map构造函数中填充一个多维整数数组,但它无法正常工作。这是我在“Map.h”中的代码(不相关的代码已被删除)。classMap{private:intmapArray[15][20];};还有我来自Map.cpp的代码Map::Map(){mapArray={{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19},{20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39},{40,41,42,43,44,45,46,4

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++ - 我可以将 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++ - 如何禁止简单的赋值 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++ - 标准是否保证 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(