[英]Condensing for 5 for-loops that display an array into 1?
Is there a way to condense five for-loops into one and have them display different variables and letters for each? 有没有一种方法可以将五个for循环压缩为一个,并让每个循环显示不同的变量和字母? Currently, I have one loop with five other loops and if/else to keep it condense, but this seems redundant and defeats the very purpose of making the loop. 目前,我有一个循环和其他五个循环,如果要保持凝聚,则使用if / else,但这似乎是多余的,并且使实现该循环的目的无法实现。
So I decided to post the whole source code so people can understand what I am trying to get at more. 因此,我决定发布整个源代码,以便人们可以了解我想要进一步了解的内容。 This is a program that creates 100 random grades everytime it runs and I have to sort them, then display them. 这个程序每次运行都会创建100个随机成绩,我必须对它们进行排序,然后显示它们。 I am aware I could do 5 for loops, but I want to write code that is more condensed and efficient. 我知道我可以为循环做5个,但我想编写更简洁高效的代码。
The hard part is writing a loop that can display 5 arrays consistently even though the size of the array changes every run. 困难的部分是编写一个循环,该循环可以始终显示5个阵列,即使每次运行都会改变阵列的大小。
#include <iostream>
#include <vector>
using namespace std;
int main(){
int grades[100];
int sizeA=0, sizeB=0, sizeC=0, sizeD=0, sizeF=0;
std::vector<int> gradeA, gradeB, gradeC, gradeD, gradeF;
srand(time(NULL));
for(int i = 0; i < 100; i++){
grades[i] = rand() % 100 + 1;
}
for(int i = 0; i < 100; i++){
if (grades[i] < 100 || grades[i] > 0){
if (grades[i]>=90)
gradeA.push_back(grades[i]);
else if (grades[i]>=70)
gradeB.push_back(grades[i]);
else if (grades[i]>=50)
gradeC.push_back(grades[i]);
else if (grades[i]>=20)
gradeD.push_back(grades[i]);
else if (grades[i])
gradeF.push_back(grades[i]);
} else {
cout << "uhh.. ";
return(0);
}
}
sizeA = gradeA.size();
sizeB = gradeB.size();
sizeC = gradeC.size();
sizeD = gradeD.size();
sizeF = gradeF.size();
/**toggle ? showgrades(gradeA, size) : (int?)null;**/
}
How about using a function to do the looping and call it with the required information 如何使用函数进行循环并使用所需的信息进行调用
void printGrades(const std::vector<int>& grades, char level) {
cout << num << " " << level << " students: ";
for(int i = 0; i < grades.size(); i++){
cout << grades[i] << " ";
cout << endl;
}
So when you want to print them all: 因此,当您要全部打印它们时:
printGrades(gradeA, 'A');
printGrades(gradeB, 'B');
printGrades(gradeC, 'C');
printGrades(gradeD, 'D');
printGrades(gradeF, 'F');
If I were you, I would create a class Student, and then a 2D array, where every row would represent the student's category, and the number of columns of a row, the number of students. 如果您是我,我将创建一个“学生”类,然后创建一个2D数组,其中每行将代表学生的类别,一行的列数即学生数。
That could be represented as a fixed-sized array of size 5, where every cell would be a std::vector
of class Student
. 这可以表示为大小为5的固定大小的数组,其中每个单元格都是class Student
的std::vector
。
Minimal working example: 最小的工作示例:
#include <iostream>
#include <vector>
using namespace std;
class Student {
public:
Student(int grade, char category) : grade(grade), category(category) {}
int getGrade(void) { return grade; }
char getCategory(void) { return category; }
private:
int grade; // 0, ..., 20
char category; // A, ..., F
};
int main(void) {
vector<class Student> std[5]; // cell 0 is for A students, ..., cel 4 is for F students
std[0].push_back({20, 'A'}); std[0].push_back({19, 'A'});
std[1].push_back({15, 'B'});
std[2].push_back({17, 'C'}); std[2].push_back({17, 'C'});
std[3].push_back({14, 'D'});
std[4].push_back({15, 'F'});
for (int i = 0; i < 5; ++i) {
if(std[i].size()) {
cout << std[i].size() << " " << std[i][0].getCategory() << " students: ";
for (auto& student : std[i]) {
cout << student.getGrade() << " ";
}
cout << endl;
}
}
return 0;
}
Output: 输出:
2 A students: 20 19
1 B students: 15
2 C students: 17 17
1 D students: 14
1 F students: 15
Appendix: 附录:
I understand that you want to make the code compact, but this: 我了解您想使代码紧凑,但这是:
for(int a = 0; a < sizeA; a++){
cout << gradeA[a] << " ";
} aS = 1; bS = 0; cout << endl;
is not cool, since it's not readable for the reader. 这不是很酷,因为它对于读者来说是不可读的。 I suggest you to change it to: 我建议您将其更改为:
for(int a = 0; a < sizeA; a++){
cout << gradeA[a] << " ";
}
aS = 1;
bS = 0;
cout << endl;
since a reader expects to see no code after the closing curly bracket. 因为读者希望在大括号后看不到任何代码。 That's a general statement. 这是一般性的说法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.