int array[30];
array[9] = 1;
array[5] = 1;
array[14] = 1;
array[8] = 2;
array[15] = 2;
array[23] = 2;
array[12] = 2;
//...
Run Code Online (Sandbox Code Playgroud)
有没有办法初始化数组类似于以下?
int array[30];
array[9,5,14] = 1;
array[8,15,23,12] = 2;
//...
Run Code Online (Sandbox Code Playgroud)
注意:在实际代码中,最多可以有30个插槽需要设置为一个值.
它是使用您的任何一种选择好的做法/既包括警卫和#pragma once在每一个头文件,或者只是那些东西,例如类的声明?
我很想把它放在每个标题中,但我担心它会不需要,只会增加编译时间.什么是良好做法或共同做什么?
让我澄清一下:我理解两者之间的区别.我问的是,经验丰富的程序员是在每个文件中使用它还是只在需要它的文件中使用它.
为什么在编译时Sample 1,它会使用我的所有内存并崩溃我的计算机,而Sample 2不会立即编译?
样本1:
class Foo
{
int a = 0;
};
class Test
{
Foo foo[4000000] = {};
};
int main()
{
Test t;
}
Run Code Online (Sandbox Code Playgroud)
样本2:
class Foo
{
int a = 0;
};
int main()
{
Foo foo[4000000] = {};
}
Run Code Online (Sandbox Code Playgroud)
最后,有没有办法在编译时阻止样本1使用大量的RAM?我正在使用gcc version 5.3.0和我编译上面的-std=c++11.请注意,class Test只需要16 MB的内存.
任何
我正在帮助用C++编写一个用于Nintendo DS的游戏(它有大约3MB的RAM).对于界面中的所有菜单,过去通过调用void函数创建"按钮",该函数将背景图块设置为按钮.整个界面至少有30个按钮.现在我创建了一个按钮类,它存储了它的位置,标签以及其他数据值.现在我的问题是:
在程序离开对象的范围后,所有这些新按钮对象是否会影响RAM使用(或其他性能方面)?
或者,一旦程序离开创建的函数,对象是否会自动被丢弃?
这是一些代码:
#include "Button.h"
void titlescreen() //Called to create main menu
{
Button singlePlayer = Button(4, 5, "Single Player");
//Creates button at coord (4,5)
Button multiPlayer = Button(4, 8, "Multi Player");
bool chosen = false; //Whether an option has been clicked
while(!chosen)
{
//Menu stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
Button.h:
#include <stdio.h>
#ifndef BUTTON_H
#define BUTTON_H
class Button
{
public:
int length;
int x, y;
bool isColored;
void setColored(bool);
void setDefault();
button(int, int, const char * const); //Constructor …Run Code Online (Sandbox Code Playgroud) 我使用c ++编写Nintendo DS(4MB RAM).我已经制作了一个按钮类来在UI中显示按钮,就像我在这里描述的那样.为方便起见,我有四个独立的构造函数.我可以将它们全部压缩成一个构造,但这样会很不方便,因为我需要在构造它时使用所有参数.我的问题是:
在程序运行时,多个重载构造函数是否会占用每个对象更多的内存,或者编译器会自动剥离每个对象不需要的其他3个不必要的构造函数?
任何帮助表示赞赏.以下是构造函数的参数和解释:
Button::Button(int x, int y, const char * const label)
{
//Set visibility to true as a default
//Set length to String length
}
Button::Button(int x, int y, const char * const label, bool isVisible)
{
//Set the length to the string length
}
Button::Button(int x, int y, const char * const label, int length)
{
//set visibility to true as a default
}
Button::Button(int x, int y, const char * const label, …Run Code Online (Sandbox Code Playgroud) 我用C++编写了Nintendo DS编码,但这应该适用于所有c ++.
我已经知道了switch语句,但是我需要创建一组if,then和else,它们有多个参数:
void doSomething(int number)
{
...
}
bool left = true;
bool right = false;
bool top = false;
bool bottom = false;
if (left && top && right)
doSomething(1);
else if (top && right && bottom)
doSomething(2);
else if (left && right && bottom)
doSomething(3);
else if (left && top && bottom)
doSomething(4);
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
有没有办法(使用C++ 11)在类构造函数中初始化多维数组?
这会将整个数组初始化为零还是仅初始化第一维?
class Example
{
public:
int array[100][100];
Example():array{}
{
};
};
Run Code Online (Sandbox Code Playgroud)
最后,这将使用枚举类型吗?
我有以下内容:
...
#define DIRT 4
#define PLANKS 5
#define WOOD_PLANKSs 5
#define BRICKS 6
...
Run Code Online (Sandbox Code Playgroud)
我需要删除其中一行与其前面的行相同的行(例如,删除木板,因为木制图具有相同的编号,5).
有没有一种方法可以在linux(或Windows)脚本中执行此操作,也许使用if then语句?
c++ ×7
c++11 ×2
constructor ×2
devkitpro ×2
if-statement ×2
memory ×2
ram ×2
arrays ×1
c ×1
command-line ×1
compilation ×1
include ×1
large-data ×1
linux ×1
object ×1
pragma ×1
replace ×1