[英]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;
}
}
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.