简体   繁体   English

如何防止上一个矢量项打印?

[英]How do I prevent last vector item from printing?

I am trying to prevent the last item of a vector from printing. 我试图阻止矢量的最后一项打印。 How do I modify my code to make this happen? 如何修改我的代码才能实现这一目标?

void lAverage(){
    cout << endl << "The average of: ";
    for (int i = 0; i < gAverage.size(); ++i){
        if (i == gAverage.size()) {
            cout << gAverage[i]; // I have to modify something here.
        } else{
            cout << gAverage[i] << ", ";

        }
    }
    cout << " = " << average << endl;
}

instead of printing: the average of: 3, 2, 1, 0 = 2 而不是打印: 平均值:3,2,1,0 = 2

I'd rather: The average of : 3, 2, 1 = 2 我宁愿: 平均值:3,2,1 = 2

No comma on the last one and the 0 removed. 最后一个没有逗号,删除了0。

Just stop your iteration a step earlier: 只需提前停止迭代:

int size = gAverage.size() - 1;
for (int i = 0; i < size; ++i){
    if (i == size - 1) {
        cout << gAverage[i];
    } else{
        cout << gAverage[i] << ", ";

    }
}
cout << " = " << average << endl;

There is no case where 没有案例

(i == gAverage.size())

Because i is always smaller than gAverage.size() because the range of the for loop is from 0 .. until gAverage.size()-1 因为i总是比gAverage.size()小,因为for循环的范围是从0到gAverage.size() - 1

You should check 你应该检查一下

if (i == gAverage.size()-1)

If you want to print to an output stream with intervening commas, but at the same time exclude the last two item in the container (for whatever reason), here is a solution using the std::copy algorithm: 如果要打印到带有插入逗号的输出流,但同时排除容器中的最后两项(无论出于何种原因),这里是使用std::copy算法的解决方案:

#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

void lAverage()
{
    // make sure there are at least 2 items
    if (gAverage.size() >= 2)
    {
        auto itLast = gAverage.end() - 2;
        cout << endl << "The average of: ";

        // copy item to output stream with intervening comma up until
        // two before the last item  
        copy(gAverage.begin(), itLast, ostream_iterator<int>(cout, ","));

        // print the item before the last and the average
        cout << *itLast << " = " << average << endl;
   }
}

Live Example 实例

You need to iterate until you reach the element before the last one : 您需要迭代,直到到达最后一个元素之前的元素:

for (int i = 0; i < gAverage.size()-1; ++i){

and

if (i == gAverage.size()-2) {

careful, it's -2 because you'll never reach gAverage.size()-1 小心,它是-2,因为你永远不会达到gAverage.size()-1

Two questions here: how to print one element less than the total number in the container, and how to make the last element printed not be followed by a comma. 这里有两个问题:如何打印一个小于容器中总数的元素,以及如何使最后一个元素打印后不跟逗号。

To meet both requirements without using a conditional, I'd stop the loop two elements back 为了在不使用条件的情况下满足这两个要求,我将停止循环两个元素

int last = gAverage.size() - 2;
for (int i = 0; i < last; ++i)
{
    cout << gAverage[i] << ", ";
}
cout << gAverage[last];

cout << " = " << average << endl;

The loop prints all values that will be followed by the comma. 循环打印逗号后面的所有值。 The next cout statement prints the second last element of the array. 下一个cout语句打印数组的倒数第二个元素。

This avoids the silliness of a condition in the loop to detect the last element being printed. 这避免了循环中条件的愚蠢,以检测最后打印的元素。

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

相关问题 如何防止我的程序打印多余的斜线? - How do I prevent my program from printing an extra slash? 如何从具有特定值的 stl 向量中删除项目? - How do I remove an item from a stl vector with a certain value? 在这种情况下,如何从向量中删除项目? - How do I erase an item from a vector in this situation? 防止更改char *向量中的最后一个元素 - Prevent last element in char * vector from changing 如何在for循环中擦除向量中的对象时,如何防止出现分段错误? - How do I prevent a segmentation fault from occuring when an object in a vector is erased while in a for loop? 如何使用向量中的信息填充数组并从数组中选择一个随机项? - How do I populate an array with information from a vector and select a random item from the array? 如何使用 std::move 从向量中删除最后一项并更新向量大小 - how to remove last item from vector using std::move and update vector size 如何在代码中显示模拟,为每个搜索 session 打印数组并最后打印总比较? - How do I show the simulations inside the code printing the array for each search session and printing the total comparison at last? 如何使用向量中的数据创建向量 - How do I create a vector using the data from the vector 如何从源向量<>构建搜索结果的向量<>? - How do I build a vector<> of search results from a source vector<>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM