Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将向量写入c+中的csv+;_C++ - Fatal编程技术网

C++ 将向量写入c+中的csv+;

C++ 将向量写入c+中的csv+;,c++,C++,我试图在C++中编写一个向量到.cv最终,但是.CSV文件的格式错误。 我目前正在这样做: ofstream myfile(rtn); int vsize = returns.size(); for(int n=0; n<vsize; n++) { myfile << returns[n] << '\t'; myfile << "," ; } 全部在一列中,但在不同的行上。有人对我将如何着

我试图在C++中编写一个向量到.cv最终,但是.CSV文件的格式错误。 我目前正在这样做:

    ofstream myfile(rtn);
    int vsize = returns.size();
    for(int n=0; n<vsize; n++)
     {
     myfile << returns[n] << '\t';
     myfile << "," ;
     }
全部在一列中,但在不同的行上。有人对我将如何着手做这件事有什么建议吗

谢谢大家!

流myfile(rtn)的
ofstream myfile(rtn);
int vsize = returns.size();
for (int n=0; n<vsize; n++)
{
    myfile << returns[n] << endl;
}
int-vsize=returns.size();
对于(int n=0;n您可以隐式循环向量。将std::cout替换为std::ofstream,以输出到文件

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

int main() {

    std::vector<std::string> v;
    v.push_back("a");
    v.push_back("b");
    v.push_back("c");

    std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));


    return 0;
}
#包括
#包括
#包括
#包括
int main(){
std::向量v;
v、 推回(“a”);
v、 推回(“b”);
v、 推回(“c”);
std::copy(v.begin()、v.end()、std::ostream_迭代器(std::cout,“\n”);
返回0;
}

你所展示的示例输出没有逗号,你想把它放在哪里?简单的答案是在正确的地方放一个<代码> \n>代码>而不是<代码> \t>代码>考虑用“STD::Endl”<代码替换'',但是Endl通常是因为它刷新了换行符之后的缓冲区。如果您也有原因,您只能手动刷新缓冲区,否则由实现决定。
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

int main() {

    std::vector<std::string> v;
    v.push_back("a");
    v.push_back("b");
    v.push_back("c");

    std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));


    return 0;
}