64,687
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <string.h>
#define BUFMAXSIZE (1024*1024)
using namespace std;
void strToMap(std::string str, std::map<std::string, float> &featMap);
int main(void){
vector<map<string,float> > feats;
ifstream dataFileIn("lr.txt", ios::in);
int k = 1;
if (dataFileIn.fail())
{
std::cout << "error: data feat open failure\n";
}
char *buffer = (char*)malloc(BUFMAXSIZE * sizeof(char));
while (!dataFileIn.eof())
{
memset(buffer, 0, BUFMAXSIZE);
dataFileIn.getline(buffer, BUFMAXSIZE);
map<string, float> temp;
strToMap(buffer, temp);
feats.push_back(temp);
}
while (k--)
{
for (map<string, float> feat : feats)
{
for(map<string,float>::iterator iter=feat.begin(); iter != feat.end(); iter++)
{
cout<<iter->first<<"\n";
}
cout<<"\n";
}
}
free(buffer);
dataFileIn.close();
return 0;
}
void strToMap(std::string str, std::map<std::string, float> &featMap)
{
const char *sep = ",";
char *cbuff = new char[str.size() + 100];
strncpy(cbuff, str.c_str(), str.size() + 1);
char *p = nullptr;
char *token = strtok_r(cbuff, sep, &p);
while (token)
{
featMap.insert(std::pair<std::string, float>(token, 1.0));
token = strtok_r(NULL, sep, &p);
}
delete cbuff;
return;
}
T-L:195-34,T-L:196-55,T-L:197-20,T-L:198-29,C3:2675,C3:2675-C3:672
T-L:195-34,T-L:196-55,T-L:197-20,T-L:198-29,C3:2676,C3:2676-C3:672
T-L:195-34,T-L:196-55,T-L:197-20,T-L:198-29,C3:2675,C3:2675-C3:672