繁体   English   中英

如何查看向量中的内容?

[英]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个值,所以我插入了100100。然后它要求3个值,所以我用80 90 100,当我期望80 90 100时,所有作业位置的cout都显示为80。实际程序最终cout按预期返回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读取3次而不是一次,即在循环的每次迭代中从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算法添加“更多C ++”样式的解决方案。

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

还有你的问题

如何查看向量中的内容?

解决方案是,迭代向量中的元素,并将向量值复制到ctd::cout 为此,我们将使用std::ostream_iterator

请注意:我总是使用限定名称lke 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM