将 CSV 文件中的双倍值C++到矢量中

C++ double values from CSV file into vector

本文关键字:C++ CSV 文件      更新时间:2023-10-16

我有一个代码,它在我的 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;