【发布时间】:2016-08-23 18:57:24
【问题描述】:
两者有什么区别:
int size = 10000;
和
const int size = 10000;
此代码处理数组,其中大小变量名称将作为数组 [size] 进入数组。
我如何在我的代码中使用它:
int main() {
int size = 50000; // here is where I had const before
int * items;
items = new int [size];
//random array for bubbleSort
for (int i = 0; i < size; i++) {
items[i] = rand();
}
clock_t start, end;
assert((start = clock()) != -1);
sort1(items, size); //bubbleSort
end = clock();
cout << "bubbleSort(random): " << (double)(end - start)/CLOCKS_PER_SEC << " seconds" << endl;
【问题讨论】:
-
你帖子里的代码还不够读者尝试编译。请设法发布minimal reproducible example
-
我投票决定将此问题作为离题结束,因为它非常基础,可以通过简单的网络搜索“什么是 const 值”来解决。
标签: c++ arrays integer constants