Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 存储变量的更有效方法是什么?_C++_Variables_Memory_Int_Constexpr - Fatal编程技术网

C++ 存储变量的更有效方法是什么?

C++ 存储变量的更有效方法是什么?,c++,variables,memory,int,constexpr,C++,Variables,Memory,Int,Constexpr,我正在制作一个音乐节目,根据音程调出半音音阶的音符。这些区间变量(h-半步、w-全步和wh-全步和半步)将用于确定标度增量(主要=WWHWWH),随后将用于测量字符串向量的区间长度,以潜在输出测量值,如“3个全步和半步” 我想知道存储简单变量的更有效的方法是什么,因为我最终想用它制作一个手机应用程序,并希望它在电池/内存方面尽可能简单。我还在学习。以下是我的想法: int H = 1; int W = 2; int WH = 3; Int Fiv = 5; Int Sev = 7; 或 我主要

我正在制作一个音乐节目,根据音程调出半音音阶的音符。这些区间变量(h-半步、w-全步和wh-全步和半步)将用于确定标度增量(主要=WWHWWH),随后将用于测量字符串向量的区间长度,以潜在输出测量值,如“3个全步和半步”

我想知道存储简单变量的更有效的方法是什么,因为我最终想用它制作一个手机应用程序,并希望它在电池/内存方面尽可能简单。我还在学习。以下是我的想法:

int H = 1;
int W = 2;
int WH = 3;
Int Fiv = 5;
Int Sev = 7;

我主要感兴趣的是初始化的差异将如何影响内存和性能

我知道我不应该把所有的东西都放在主屏幕上,但这是一项正在进行的工作,而且我对编程显然是新手,所以请看一下布局。。这是目前正在使用的代码

#include <algorithm> 
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector> 
#include <map>

const std::vector<std::string> st_sharps{"C","C#","D","D#","E","F","F#","G","G#","A","A#","B" };
const std::vector<std::string> st_flats{"C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B" };

struct steps{ int maj = 0; int min = 0;} step;
constexpr int H = 1;
constexpr int W = 2;
constexpr int Tre = 3;
constexpr int Fif = 5;
constexpr int Sev = 7;
const int size = st_flats.size();
const std::vector<int> Major = { W, W, H, W, W, W, H };

struct circle{
std::stringstream sharp;
std::stringstream flat;
std::stringstream minor;
std::stringstream dimin; };

struct scales{
circle fifths;
std::stringstream maj;
std::stringstream min; } scale;

int main(){
    //Circle of Fifths
   for (int j = 0; j < size; j++){
        int five = j * Sev;
        scale.fifths.sharp << st_sharps[five % size] << " ";        
        scale.fifths.flat << st_flats[five % size] << " ";
        scale.fifths.minor << st_sharps[((size - Tre) + five) %  size] << " ";
        scale.fifths.dimin << st_sharps[((size - H) + five) % size] << " ";
    }

    std::cout << "Circle of Fifths:\n";
    std::cout << "Major >> Relative Minor >> Diminished " << std::endl;
    std::cout << "Maj: " << scale.fifths.sharp.str() << std::endl;
    std::cout << "Min: " << scale.fifths.minor.str() << std::endl;
    std::cout << "Dim: " << scale.fifths.dimin.str() << std::endl;
    std::cout << "\nflats: " << scale.fifths.flat.str() << "\n" << std::endl;

    //Major and Minor Scales
    for (int i = 0; i < Major.size(); i++) {
        scale.maj << st_sharps[step.maj] << " ";
        scale.min << st_flats[((size - Tre) + step.min) % size] << " ";
        step.maj += Major[i];
        step.min += Major[(i + Fif) % Major.size()];
    }
    std::cout << "C Major:\n" << scale.maj.str() << "\n" << std::endl;
    std::cout << "A Minor:\n" << scale.min.str() << "\n" << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
常数std::向量st#u锐化{“C”,“C#”,“D”,“D#”,“E”,“F”,“F#”,“G”,“G#”,“A”,“A#”,“B”};
常数std::向量st_单位{“C”、“Db”、“D”、“Eb”、“E”、“F”、“Gb”、“G”、“Ab”、“A”、“Bb”、“B”};
结构步骤{intmaj=0;intmin=0;}步骤;
constexpr int H=1;
constexpr int W=2;
constexpr int Tre=3;
constexpr int Fif=5;
constexpr int Sev=7;
const int size=st_flats.size();
const std::vector Major={W,W,H,W,W,H};
结构圆{
std::stringstream-sharp;
std::stringstream flat;
性病:小调;
std::stringstream dimin;};
结构尺度{
圈五分之一;
std::stringstream maj;
std::stringstream min;}比例;
int main(){
//五分之一圈
对于(int j=0;jscale.fifths.sharp我会选择一个表达“W”是“H”的两倍”的版本,这是最好的方式。因此,我更喜欢的方式是:

constexpr int H = 1;
constexpr int W = 2*H;
constexpr int WH = W+H;

<>请注意,你的版本<代码> int W= H++< /C>不是你想要的,因为<代码> H++< /COD>不等于<代码> H+ 1 ;它实际上等于<代码> int W= H;H= H+ 1 。

我认为你需要在研究这些重量级问题之前先学习C++运算符,因为你的两个备选版本。相同代码的s不相等,产生不同的结果。
H
W
WH
在两个备选版本中具有不同的值。如果您要使用
constexpr
,那么您应该在示例中使用。您不应该垃圾邮件发送一百万个不必要的标记,并且您应该尝试向at可以用事实而不仅仅是观点来回答。如果你使用constexpr,你会很快得出@SamVarshavchik指出的结论。第一个很清楚,第二个有不同的含义,因为
int
post increment变异了它的操作数,我只见过第三个用来轻松定义数学常数,即
constexpr double tau=pi+pi;
H是一半。W是完整的。WH是完整的,音乐间隔是一半。不是像一些人可能认为的那样高和宽。我在手机上,所以我没有时间测试这些,但我想知道什么是最有效的内存管理。所有这些答案都是我问minu的确切原因RichardChristopher你的问题毫无意义。在你的例子中,你的问题所要求的存储没有区别,你的例子也没有说明你想做什么。如果你编辑你的问题,用
constexpr
vector
map
包含一个例子或想要的用法,那么y你可以希望得到一个有意义的答案。它最终会替换字符串映射中的数字,向量{“调和”,{0,2,3,5,7,8,11,12},…我还是新手。我确信这不会起作用..可能是调和{+=W,+=H,+=W,+=W,+=H,+=WH+=H}…差不多吧。我还是新来的。רטיאבידן-那会是什么样子?这会被取消吗?@RichardChristopher你现在有2张重新投票,另外3张,你赢了。如果一天内没有发生,请告诉我。
constexpr int H = 1;
constexpr int W = 2*H;
constexpr int WH = W+H;