【发布时间】:2020-10-21 03:34:51
【问题描述】:
此代码 C6262 警告不断出现,导致程序出现问题,我尝试寻找可能的解决方案,但我很难理解。如果有人能帮我解决这个问题,如果你能指出我可以改进的代码中的任何错误部分,我将不胜感激。
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
class sorting {
private:
int size, elements;
int arr[5000], x;
public:
void sort() {
cout << "Enter number of desired elements for the 1st set" << ">"; cin >> elements;
arr[elements];
half(); cout << endl;
bubble();
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
}
void half() {
for (int i = 0; i < elements / 2; i++) {
arr[i] = i + 1;
}
for (int i = elements / 2; i < elements; i++) {
arr[i] = rand();
}
cout << "This is the elements of the 1st set: ";
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
}
void random() {
for (int i = 0; i < elements; i++) {
arr[i] = i + 1;
}
random_shuffle(&arr[0], &arr[elements]);
cout << "This is the elements of the 2nd set: ";
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
}
void ascend_descend() {
int x = elements / 2;
arr[0] = x;
for (int i = 0; i < elements / 2; i++) {
arr[i + 1] = x - 1;
x--;
}
for (int i = elements / 2; i < elements; i++) {
arr[i] = i + 1;
}
cout << "This is the elements of the 3rd set: ";
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
};
void bubble() {
for (int i = 0; i < elements; i++) {
int temp;
for (int j = i + 1; j < elements; i++) {
if (arr[j] < arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
};
}
};
int main()
{
sorting sortObject;
sortObject.sort();
return 0;
}
【问题讨论】:
-
This warning indicates that stack usage that exceeds a preset threshold (constant_2) has been detected in a function.docs.microsoft.com/en-us/cpp/code-quality/c6262?view=vs-2019 考虑一个向量。此外,arr[elements];并没有按照您的想法执行,您的程序也不会按原样编译。 -
您的代码格式还有很多不足之处。您不应在函数末尾的
}之后放置分号。 -
你认为
arr[elements];行是做什么的?它不会调整数组的大小...它只返回对索引为“elements”的元素的引用...但您不对其进行任何操作,因此代码是多余的。
标签: c++