从C ++输出中删除逗号 [英] Removing a comma from a c++ output

查看:128
本文介绍了从C ++输出中删除逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个程序,它从最大到最小对向量中的数字进行排序,它的工作原理很棒,但是困扰我的唯一一件事就是试图从最后一个数字中删除逗号。这是我的代码

  #include< iostream> 
#include< vector>
#include< cstdlib>
#include< algorithm>
使用命名空间std;

int main(){

vector< int> vi1,vi2,vi3;
srand(987);

for(int i = 0; i< 10; ++ i)vi1.push_back(rand()%10);
sort(vi1.begin(),vi1.end());

for(int i = 0; i <10; ++ i)vi2.push_back(rand()%10);
sort(vi2.begin(),vi2.end())

while(!vi1.empty()&&!vi2.empty()){
if(vi1.back()> = vi2.back()){
vi3.push_back(vi1.back());
vi1.pop_back();
}
else {
vi3.push_back(vi2.back());
vi2.pop_back();
}
}

while(!vi1.empty()){
vi3.push_back(vi1.back());
vi1.pop_back();
}
while(!vi2.empty()){
vi3.push_back(vi2.back());
vi2.pop_back();
}

for(auto i = vi3.begin(); i!= vi3.end(); ++ i)
cout<< * i<< ,;
cout<< \nBye ...<<恩德尔
返回0;
}

这是输出

  9、9、9、8、8、8、6、6、3、3、3、3、2、2、2、1、1、1、0 ,0,
再见...

您可以在最后一个0之后看到语法上没有意义的逗号。如何删除逗号?

解决方案

而不是删除,最简单的方法就是打印逗号仅适用于除第一个值以外的其他值:

  for(auto i = vi3.begin(); i!= vi3.end( ); ++ i){
if(i!= vi3.begin()){
cout<< ,;
}
cout<< *一世;
}

我经常使用这个惯用法来格式化例如SQL参数列表,或类似的参数,在最后一个元素之后不希望使用分隔符,但对于其他元素则不需要。

还有其他方法可以检测第一个元素(例如,使用 bool 变量在循环开始前初始化为 true ,并设置为 false 第一次迭代。)

对于您的示例,似乎只检查 vi3.begin()是最简单,最自然的方法。 / p>

这是伪代码中的通用变体:

  bool isFirstOutput = true ; 
for_each(列表中的元素){
if(不是isFirstOutput){
打印定界符;
}
打印元素;
isFirstOutput = false;
}


I wrote this program that sorts numbers in vectors from greatest to least, it works great but the only thing that is bugging me is trying to remove the comma from the last number. Heres my code

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main() {

    vector<int> vi1, vi2, vi3;
    srand(987);

    for (int i = 0; i < 10; ++i) vi1.push_back(rand() % 10);
    sort(vi1.begin(), vi1.end());

    for (int i = 0; i < 10; ++i) vi2.push_back(rand() % 10);
    sort(vi2.begin(), vi2.end())

    while(!vi1.empty() && !vi2.empty()) {
        if(vi1.back()>=vi2.back()) {
            vi3.push_back(vi1.back());
            vi1.pop_back();
        }
        else {
            vi3.push_back(vi2.back());
            vi2.pop_back();
        }
    }

    while(!vi1.empty()) {
        vi3.push_back(vi1.back());
        vi1.pop_back();
    }
    while(!vi2.empty()) {
        vi3.push_back(vi2.back());
        vi2.pop_back();
    }

    for (auto i = vi3.begin(); i != vi3.end(); ++i)
        cout << *i << ", ";
    cout << "\nBye..." << endl;
    return 0;
}

and here is the output

9, 9, 9, 8, 8, 8, 6, 6, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 
Bye...

as you can see after the last 0 there is a comma which doesn't make sense grammatically speaking. How can I remove that comma?

解决方案

Rather than removing the simplest way is just to print commas only for other values than the first:

for (auto i = vi3.begin(); i != vi3.end(); ++i) {
    if(i != vi3.begin()) {
        cout << ", ";
    }
    cout << *i;
}

I am using this idiom regularly to format e.g. SQL parameter lists, or similar where a delimiter isn't wanted after the last element but for any others.
There are other ways to detect the first element (e.g. using a bool variable initialized to true before the loop starts, and set to false upon the first iteration).
For your example it seems just checking for vi3.begin() is the easiest and most natural way.

Here's the generic variant in pseudo code:

bool isFirstOutput = true;
for_each(element in list) {
    if(not isFirstOutput) {
        print delimiter;
    }
    print element;
    isFirstOutput = false;
}

这篇关于从C ++输出中删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆