使用动态内存分配
Using Dynamic Memory Allocation
所以我被告知要创建一个数组,该数组将从用户那里接受 10 个整数,将其存储到一个数组中,然后使用指针冒泡排序对这些值进行升序排序。
我相信我已经成功完成了这么多,但是我在第二部分遇到了问题。
"Dynamically Allocate another array of 10 integers. Copy elements from the first into the second, but in reverse order (i.e. descending order). Display the elements of the first and second array in order and deallocate the dynamically allocated array."
我能够按顺序显示第一个数组,而且我知道要释放数组你必须使用删除函数,但我不太确定如何构造动态数组。
*我没有包含这些函数,因为我认为它们对于这部分不是必需的,但如果我这样做,那么我也会 post 它们。
提前感谢您的任何建议和说明。
#include <iostream>
using namespace std;
void sortArray(int * , int);
void showArray(const int * , int);
int binarySearch(const int *, int, int);
int main(void)
{
int const MAX_NUM = 10;
int numbers [MAX_NUM];
int counter;
int findval;
int index;
char again;
cout<< "Please enter 10 integer values."<< endl;
for(counter=0; counter< MAX_NUM ; counter++)
{
cout << "Enter a value for "<< counter+1 << ": ";
cin >> *(numbers+counter);
}
sortArray(numbers, 10);
cout << endl << "The values in ascending order are: " << endl;
showArray(numbers, 10);
do
{
cout<< endl << "Enter the value you are searching for: ";
cin >> findval;
cout << endl;
index = binarySearch(numbers , MAX_NUM , findval);
// Display the results of the search.
if (index == -1)
cout << "Number was not found." << endl << endl;
else
cout << "Number "<< findval<<" found in position " << index + 1 << endl << endl;
// Does the user want to do this again?
do
{
cout << "Would you like to look up another number? (y/n) ";
cin >> again;
}
while(again != 'y' && again != 'Y' && again != 'n' && again != 'N');
}
while (again == 'Y' || again == 'y');
cout<< endl << "Thank You. Press the return key to continue...";
cin.get();
cin.ignore();
return 0;
}
运算符new
应该用于分配内存。对于 dealloaction 使用 delete
.
从分配内存开始:
int * dynArr = NULL; // pointer to work with dynamic array
dynArr = new int[MAX_NUM]; // allocation of memory
然后检查是否分配了内存,如:
if( dynArr != NULL )
{
// do something
}
else
{
// report about problem and do not use pointer
}
并使用复制元素的函数,例如:
void reversCopy(const int * source, int * destination, int number)
// Function for copying numbers from one array (memory) to other
// in the revers order (first element goes to the last position).
// source - pointer to array where numbers will be read
// destination - pointer to array where numbers will be written
// number - number of elements to be copyed
{
for(int i = 0; i < number; i++)
{
destination[i] = source[number - 1 - i];
}
}
最终,使用运算符释放动态内存:
delete[] dynArr;
dynArr = NULL;
之后不要使用 dynArr
。
动态内存管理应该使用 C++ 标准 类 和 smart pointers or containers 可用的概念来完成。
正确使用 C++ 语言并不需要您使用 new
/delete
来处理您实际需要涵盖的大多数用例。
要动态分配数组,您需要构建如下代码:
int *arr = new int[size]; // you have to remember to free memory when you won't need this array anymore - use delete[] achieve this
size 变量不必是 const,编译器在编译期间不需要知道它的值。你可以要求用户提供尺寸 :) 不要反转你原来的 table 只是反转方式的刺客元素:
for (int i = 0; i < size; ++i)
{
arr[i] = numbers[size - i - 1]; <-- '-1' to not read outside of orginal array (in C++ index starts with 0)
}
如果您想逆转 table 而不使用新的,您应该访问:Reverse Contents in Array。有几种方法可以做到这一点:)
所以我被告知要创建一个数组,该数组将从用户那里接受 10 个整数,将其存储到一个数组中,然后使用指针冒泡排序对这些值进行升序排序。
我相信我已经成功完成了这么多,但是我在第二部分遇到了问题。
"Dynamically Allocate another array of 10 integers. Copy elements from the first into the second, but in reverse order (i.e. descending order). Display the elements of the first and second array in order and deallocate the dynamically allocated array."
我能够按顺序显示第一个数组,而且我知道要释放数组你必须使用删除函数,但我不太确定如何构造动态数组。
*我没有包含这些函数,因为我认为它们对于这部分不是必需的,但如果我这样做,那么我也会 post 它们。
提前感谢您的任何建议和说明。
#include <iostream>
using namespace std;
void sortArray(int * , int);
void showArray(const int * , int);
int binarySearch(const int *, int, int);
int main(void)
{
int const MAX_NUM = 10;
int numbers [MAX_NUM];
int counter;
int findval;
int index;
char again;
cout<< "Please enter 10 integer values."<< endl;
for(counter=0; counter< MAX_NUM ; counter++)
{
cout << "Enter a value for "<< counter+1 << ": ";
cin >> *(numbers+counter);
}
sortArray(numbers, 10);
cout << endl << "The values in ascending order are: " << endl;
showArray(numbers, 10);
do
{
cout<< endl << "Enter the value you are searching for: ";
cin >> findval;
cout << endl;
index = binarySearch(numbers , MAX_NUM , findval);
// Display the results of the search.
if (index == -1)
cout << "Number was not found." << endl << endl;
else
cout << "Number "<< findval<<" found in position " << index + 1 << endl << endl;
// Does the user want to do this again?
do
{
cout << "Would you like to look up another number? (y/n) ";
cin >> again;
}
while(again != 'y' && again != 'Y' && again != 'n' && again != 'N');
}
while (again == 'Y' || again == 'y');
cout<< endl << "Thank You. Press the return key to continue...";
cin.get();
cin.ignore();
return 0;
}
运算符new
应该用于分配内存。对于 dealloaction 使用 delete
.
从分配内存开始:
int * dynArr = NULL; // pointer to work with dynamic array
dynArr = new int[MAX_NUM]; // allocation of memory
然后检查是否分配了内存,如:
if( dynArr != NULL )
{
// do something
}
else
{
// report about problem and do not use pointer
}
并使用复制元素的函数,例如:
void reversCopy(const int * source, int * destination, int number)
// Function for copying numbers from one array (memory) to other
// in the revers order (first element goes to the last position).
// source - pointer to array where numbers will be read
// destination - pointer to array where numbers will be written
// number - number of elements to be copyed
{
for(int i = 0; i < number; i++)
{
destination[i] = source[number - 1 - i];
}
}
最终,使用运算符释放动态内存:
delete[] dynArr;
dynArr = NULL;
之后不要使用 dynArr
。
动态内存管理应该使用 C++ 标准 类 和 smart pointers or containers 可用的概念来完成。
正确使用 C++ 语言并不需要您使用 new
/delete
来处理您实际需要涵盖的大多数用例。
要动态分配数组,您需要构建如下代码:
int *arr = new int[size]; // you have to remember to free memory when you won't need this array anymore - use delete[] achieve this
size 变量不必是 const,编译器在编译期间不需要知道它的值。你可以要求用户提供尺寸 :) 不要反转你原来的 table 只是反转方式的刺客元素:
for (int i = 0; i < size; ++i)
{
arr[i] = numbers[size - i - 1]; <-- '-1' to not read outside of orginal array (in C++ index starts with 0)
}
如果您想逆转 table 而不使用新的,您应该访问:Reverse Contents in Array。有几种方法可以做到这一点:)