我想输出一些数字,全部用逗号分隔。在以下代码中,最后一个数字也将被“分隔”:
for(int i=1; i<=3; i++)
{
cout << i << ",";
}
有没有可能避免这种情况?
所以我只想要 1,2,3
而不是 1,2,3,
最佳答案
你必须把逗号放在不同的地方,像这样:
const int n = 5;
int tab[n] = {1,2,3,4,5};
if(n >= 1)
cout << tab[0];
for(int i=1; i<n; i++)
cout<<", "<<tab[i];
链接到 ideone.com , 可以执行代码的地方。
关于c++ - 更改最后一个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16157243/