我是 C++ 的新手。我想得到一个这样的 CSV 文件:
| S0001 | S0002 | S0003 | S0004 | ...
0 | 10.289461 | 17.012874 | |
1 | 11.491483 | 13.053712 | |
2 | 10.404887 | 12.190057 | |
3 | 10.502540 | 16.363996 | ... | ...
4 | 11.102104 | 12.795502 | |
5 | 13.205706 | 13.707030 | |
6 | 10.544555 | 12.173467 | |
7 | 10.380928 | 12.578932 | |
8 | 10.962240 | 12.615608 | |
9 | 10.690547 | 17.678212 | |
10 | 12.416197 | 13.769609 | |
... ... ... ... ...
第一列是一系列整数。单元格值是从对数正态分布生成的。
这是我的代码:
#include <iostream>
#include <vector>
#include <random>
#include <fstream>
#include <stream>
//get first row: S0001, S0002, S0003, etc
std::string GetNextNumber(int lastNum)
{
std::stringstream ss;
ss << "S";
ss << std::setfill('0') << std::setw(4) << lastNum;
return ss.str();
}
int main()
{
std::string ukey;
int ticktime;
double bid;
double len = 1000;
std::default_random_engine generator;
std::lognormal_distribution<double> distribution(0.0,1.0);
std::ofstream outFile;
outFile.open("Data_bid.csv");
for(int j=0; j<50; ++j){
ukey = GetNextNumber(j+1);
outFile<<","<<ukey;
}
outFile<<std::endl;
for(int i=0; i<len; ++i){
ticktime = i;
bid = distribution(generator)+10;
outFile<<ticktime<<","<<bid<<std::endl;
}
outFile.close();
return 0;
}
我不知道如何为其余列附加值。你能帮我或提供一些其他方法来编写 csv 吗?