且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用copy和ostream_iterator删除为向量编写的CSV文件中的结尾逗号

更新时间:2022-11-23 20:01:41

如您所见,通过std::copy复制并不能解决问题,还会输出一个额外的,.有一项建议可能会在将来的C ++ 17标准中使用: ostream_joiner ,它将完全满足您的期望.

As you observed, copying via std::copy doesn't do the trick, one additional , is output. There is a proposal that will probably make it in the future C++17 standard: ostream_joiner, which will do exactly what you expect.

但是,现在可用的快速解决方案是手动执行此操作.

However, a quick solution available now is to do it manually.

for(auto it = std::begin(*pdata); it != std::end(*pdata); ++it)
{
    if (it != std::begin(*pdata))
        std::cout << ",";
    std::cout << *it;
}