当我使用homework
检查cout
向量内部的值时,似乎只返回了homework[0]
内部的值。有人可以查看我的代码,让我知道我要去哪里了吗?
int main()
{
cout << "Please enter midterm and final: " << endl;
double midterm, gfinal;
cin >> midterm >> gfinal;
cout << "Enter all your homework grades, " << endl;
double x;
cin >> x;
// initing a vector object named homework
vector<double> homework;
// show debug on the vector
while (homework.size() != 3)
homework.push_back(x);
if (homework.size() == 0)
{
cout << endl << "You need to enter at least one number";
return 1;
}
// vector before sorting
// since cout << homework did not seem to work I could always just write a debug function to iterate over structures and print them to the console
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
// sort the vector here
sort(homework.begin(), homework.end());
// vector after sorting
//cout << homework;
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
int mid = homework.size() / 2;
cout << "The below is mid" << endl;
cout << mid << endl;
double median;
if (homework.size() % 2 == 0)
median = (homework[mid - 1] + homework[mid]) / 2;
else
median = homework[mid];
//streamsize prec = cout.precision(3);
cout << "Your course grade is "
<< 0.2 * midterm + 0.4 * gfinal + 0.4 * median << endl;
//cout.precision(prec);
return 0;
}
这是引起我困惑的特定代码:
// vector before sorting
// since cout << homework did not seem to work I could always just write a debug function to iterate over structures and print them to the console
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
// sort the vector here
sort(homework.begin(), homework.end());
// vector after sorting
//cout << homework;
cout << homework[0] << endl;
cout << homework[1] << endl;
cout << homework[2] << endl;
程序启动时要求输入2个值,所以我插入了100 100。
然后它要求3个值,所以我使用80 90 100,当我期望80 90 100时,所有作业位置的cout
都显示80。
实际程序将按预期的最终cout
返回92。
答案 0 :(得分:6)
在您的代码中:
cin
您仅从homework
读取一次,即,您正在读取一个值。然后,将读取的值插入向量homework[0]
三次。因此,homework[1]
,homework[2]
和cin >> x
包含相同的值。
请考虑将while
放在cin
循环中以从cin
读取三遍,而不是一次,即在循环的每次迭代中从vector<double> homework;
while (homework.size() < 3) {
double x;
cin >> x;
homework.push_back(x);
}
读取:< / p>
foreach($foo as $key => &$value) {
$value = 'new value';
}
答案 1 :(得分:2)
除了El Profesor指出的错误之外,要在现代C ++中迭代向量,您还要做的是:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> homework{ 70,80,90 }; // For the example
// "See what is inside the vector":
for (auto grade : homework)
std::cout << grade << '\n';
}
答案 2 :(得分:1)
因此,这些错误已得到修复。我将使用STL算法添加“更多C ++”样式的解决方案。
为填充矢量,我将使用std::copy_n
。意思是,从std::cin
中读取n个值并将其插入目标向量。
还有你的问题
如何查看向量内部?
解决方案是,遍历向量中的元素并将向量值复制到ctd::cout
。为此,我们将使用std::ostream_iterator
请注意:我总是使用限定名称,例如std::cout
。请考虑。而且我很少使用std::endl
,因为这个总是调用flush,在大多数情况下是不需要的。另外:所有变量都应始终初始化。总是。
然后,我添加了许多评论。还请考虑。这样可以大大提高代码的可读性和质量。
请参阅:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
constexpr size_t NumberOfHomeworkGrades = 3U;
int main()
{
// Print title and ask use to enter data
std::cout << "\nCalculation of course grade\n\nPlease enter midterm and final: " << '\n';
double midterm{ 0.0 };
double gfinal{ 0.0 };
std::cin >> midterm >> gfinal;
// Ask the use to enter the howmwork grades
std::cout << "Please Enter "<< NumberOfHomeworkGrades << " homework grades\n";
// Get the data from the user and put it into the vector
std::vector<double> homeworkGrades{};
std::copy_n(
std::istream_iterator<double>(std::cin), // We will iterate over std::cin and read data
NumberOfHomeworkGrades, // Over all we read data NumberOfHomeworkGrades times
std::back_inserter(homeworkGrades) // And we psuh the data into our homeworkGrades-vector
);
// Show the vector before sorting. Simply copy all data in the vector to std::cout using the ostream_iterator
std::cout << "\n\nEntered grades before sort\n";
std::copy(homeworkGrades.begin(), homeworkGrades.end(), std::ostream_iterator<double>(std::cout, "\n"));
// Sort the vector here
std::sort(homeworkGrades.begin(), homeworkGrades.end());
// Show the vector adter sorting. Simply copy all data in the vector to std::cout using the ostream_iterator
std::cout << "\n\nSorted grades\n";
std::copy(homeworkGrades.begin(), homeworkGrades.end(), std::ostream_iterator<double>(std::cout, "\n"));
// Calculate the median
double median{ 0 };
// First calculate the mid, to do the calculation only one time and to show the result
size_t mid{ homeworkGrades.size() / 2 };
if (!homeworkGrades.empty())
if (homeworkGrades.size() % 2 == 0)
median = (homeworkGrades[mid - 1] + homeworkGrades[mid]) / 2;
else
median = homeworkGrades[mid];
// Show the result to the user
std::cout << "\n\nThe mid value is (maybe truncated): " << mid
<< "\nThe median value is: " << median
<< "\n\nYour course grade is: " << 0.2 * midterm + 0.4 * gfinal + 0.4 * median << '\n';
return 0;
}