C ++通过在每个元素后面添加逗号将向量转换为CSV [英] C++ Vector to CSV by adding Comma after each element

查看:96
本文介绍了C ++通过在每个元素后面添加逗号将向量转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vector<string> v;
v.push_back("A");
v.push_back("B");
v.push_back("C");
v.push_back("D");

for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) {
//printout 
   cout << *it << endl;

}

我喜欢在每个元素后面添加逗号如下:
A,B,C,D

I like to add a comma after each element as follow: A,B,C,D

我尝试在Google上研究,但我只找到CSV vector c>。 c>

I tried researching on Google, but I only found CSV to vector.

推荐答案

/ code> loop:

You could output the commas in your for loop:

for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) {
//printout 
   cout << *it << ", " << endl;
}

或者,您可以使用 copy 算法。

Or, you could use the copy algorithm.

std::copy(v.begin(), v.end(), std::ostream_iterator<char*>(std::cout, ", "));

这篇关于C ++通过在每个元素后面添加逗号将向量转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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