Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++;_C++_Memory_Allocation - Fatal编程技术网

C++ 最佳内存分配C++;

C++ 最佳内存分配C++;,c++,memory,allocation,C++,Memory,Allocation,我想创建一个最适合的内存分配解决方案 int main() { int input; int memoryBlock[ARRAY_SIZE] = {5, 10, 3, 9, 7}; int bestFit; cout << "Please enter the memory size you want to allocate: "; cin >> input; for (int i = 0; i < ARRAY_SI

我想创建一个最适合的内存分配解决方案

int main()
{
    int input;
    int memoryBlock[ARRAY_SIZE] = {5, 10, 3, 9, 7};
    int bestFit;

    cout << "Please enter the memory size you want to allocate: ";
    cin >> input;

    for (int i = 0; i < ARRAY_SIZE; i++){

        if (memoryBlock[i] - input < 0 ){

        }
        else {
            bestFit = memoryBlock[i];
        }

    }
    cout << bestFit;
}
intmain()
{
int输入;
int memoryBlock[ARRAY_SIZE]={5,10,3,9,7};
最佳拟合;
cout>输入;
for(int i=0;icout更改代码,使其仅在小于最佳拟合的情况下替换最佳拟合:

int bestFit = -1;

for (int i = 0; i < ARRAY_SIZE; ++i) {
    if (memoryBlock[i] - input >= 0 && memoryBlock[i] < bestFit) {
        bestFit = memoryBlock[i];
    }
}
int最佳拟合=-1;
对于(int i=0;i=0&&memoryBlock[i]