如何查看向量中的内容?

How can I see what is inside a vector?

当我使用 cout 检查 homework 向量内部的值时,它似乎只返回 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 returns 92。

在您的代码中:

double x;

cin >> x; // <-- reading from cin only once

// initing a vector object named homework
vector<double> homework;
// show debug on the vector
while (homework.size() != 3)
    homework.push_back(x); // <- inserting same value three times

您只从 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);
}

除了 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';
}

所以,错误已经被修复了。我将使用 STL 算法添加 "more-C++" 式解决方案。

为了填充向量,我将使用 std::copy_n。意思是,从 std::cin 中读取 n 个值并将它们插入到目标向量中。

关于你的问题

How can I see what is inside a vector?

解决方案是迭代向量中的元素并将向量值复制到 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;
}