将 CSV 文件中的双倍值C++到矢量中
C++ double values from CSV file into vector
我有一个代码,它在我的 CSV 文件中间获取一列,并将该列中的所有值作为字符串放入向量中。有没有办法轻松编辑此代码,以便在输入向量之前将值更改为双精度值?我的尝试不断出错。
#include<iostream>
#include<fstream>
#include<vector>
#include <sstream>
#include <string>
#include <algorithm>
std::vector<double> readStock(std::string fileName){
int counter=0;
std::vector<double> temp; //create vector
std::ifstream f(fileName);
if(f.is_open()){
std::string str;
std::getline(f,str); //skip the first row
while(std::getline(f,str)){ //read each line
std::istringstream s(str); //stringstream to parse csv
std::string val; //string to hold value
for(int i=1;i<12;++i){ //skips until we get to the column that we want
++counter;
std::getline(s,val, ',');
}
+counter;
std::getline(s,val,',');
std::size_t hold=0;
std::stod(val, &hold);
std::cout<<val<<" ";
std::cout<<hold<<" ";
}
f.close();
}
return temp;
}
int main(){
std::string ticker;
std::cin>> ticker;
ticker +=".csv";
std::cout<<ticker;
std::vector<double> stock1;
stock1=readStock(ticker);
std::cout<<st<<std::endl;
for(int i=0;i<stock.size();++i){
std::cout<<stock1[i]<<" ";
}
return 0;
}
将要存储 CSV 数字的向量声明为双精度向量?
std::vector<double> temp;
相关文章:
- 正在将csv文件读取为双精度矢量
- 如何选择在 csv 文件中输出的位置
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 无法读取C++中的 CSV 文件
- 流:CSV 文件中的换行符
- 是否可以使用 Python csv 阅读器读取使用 C++ std::setw 生成的文件?
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 将一维数组写入 CSV C++中的不同列?
- 如何从CSV加载堆栈?C++
- 如何正确格式化csv文件?
- 正在分析CSV文件-C++
- 如何获取组件和搜索 CSV 文件
- C++ 中的 CSV 文件管理(特别是设置标头和每个标头将分别具有的值)
- 无法逐行读取.csv文件
- 以CSV格式编写特征矢量Xd
- 非常快速地阅读 CSV 文件
- 读取和存储 CSV 数据
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- CSV文件之间的差异,使用getline()产生不同的结果