嘿,我目前正在做一项家庭作业,我们在其中比较不同类型的效率。但是我在访问数据元素时遇到了问题并且感觉有点笨,因为我觉得这应该是一个简单的答案。这是我的主要功能。
int main()
{
//declarations
int const MYARRAYSIZE = 100;
Sort mySort(MYARRAYSIZE);
mySort.init_array();
clock_t timeA = clock();
for(int i = 0; i < MYARRAYSIZE; i++)
{
//run tests
mySort.insertion_sort(/*whatgoeshere*/, MYARRAYSIZE );
}
clock_t timeB = clock();
clock_t diff = timeB - timeA;
system("PAUSE");
}
这是我的标题
class Sort
{
private:
int size;
int *myArray;
public:
Sort(int size);
~Sort();
friend ostream& operator << (ostream& out, const Sort& s)
{
//put code in here
}
void insertion_sort(int [], int);
void selection_sort(int [], int);
void merge_sort(int [], int);
void quick_sort(int [], int);
void partition(int [], int, int&);
void merge(int [], int, int);
void init_array();
int getSize();
};
我正在尝试访问存储在 myArray 中的数组,我知道只有类可以访问它,但我将如何访问它?
最佳答案
/* whatgoeshere*/
那里什么都没有。
从成员函数中移除参数
示例:
只需使用:
void insertion_sort( );
这知道它的 myArray
和 size
因为它们是类的数据成员
简单地称它为
mySort.insertion_sort( );
这认为您的所有成员函数都已使用 myArray
和 size
正确实现。
关于c++ - 无法从排序类 C++ 访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212340/