What I want:
From string: 24.5,0, 9, 8,FLB
Get Time : 24.5
CAN_ID: 0
RCTBSTAT: 9
R_L: 8
RCTBSTAT_Convert: FLB
getline()
和 strtok()
不能满足我的需要。
我用函数 getline()
读取了一个 csv 文件,它工作得很好。
但是,当我想拆分以以下形式获取的字符串时,这让我很困扰:0.00222,0,FALSE,345vdd,FALSE,...
并将其分配给不同的变量。
您能推荐任何替代方案吗?
struct data
{
double Time;
int CAN_ID;
int RCTBSTAT;
int R_L;
unsigned char RCTBSTAT_Convert;
unsigned char R_L_Convert;
int TRD_ID;
bool ALRT;
bool NEW;
bool EXTRAPOL;
bool GHST_POS;
int X_WDTH;
int RCS;
int REL_X;
int REC_Y;
int REL_VX;
int REL_VY;
int X_WDTH_Convert;
float REL_X_Convert;
float REL_Y_Convert;
double REL_VX_Convert;
double REL_VY_Convert;
};
int main(int argc, char *argv[])
{
ifstream inFile(argv[1]);
string line;
string headline;
string rowitems;
int linenum=0 ;
getline (inFile, headline, '\n');
for (linenum=1; getline (inFile, line); linenum++)
{
//cout << "\nLine #" << linenum << ":" << endl;
istringstream linestream(line);
istringstream headerstream(headline);
********
**help**
********
}
return 0;
}
最佳答案
你可以试试
#include <string>
#include <deque>
#include <vector>
#include "strtk.hpp"
std::string int_string = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15";
std::vector<string> int_list;
strtk::parse(int_string,",",int_list);
然后使用 vector 赋值给多个变量
编辑:您可以找到 strtk.hpp here
关于c++ - 如何拆分要分配给 C++ 中具有不同索引的单独变量的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569814/