事先不知道文档大小(MxN的矩阵),将数据读到vector<vector<double> >
容器中,有什么快速的方法?
我测试过500x500的txt文档,读进去需要18.945s,时间太长了!
/*************************
*函数名:split
*函数功能:分割字符串
*参数说明:
*src:源字符串
*separator: 分割字符
*dest:存储结果
**************************/
void split(const string& src, const string& separator, vector<string>& dest)
{
string str = src;
string substring;
string::size_type start = 0, index;
do
{
index = str.find_first_of(separator,start);
if (index != string::npos)
{
substring = str.substr(start,index-start);
dest.push_back(substring);
start = str.find_first_not_of(separator,index);
if (start == string::npos) return;
}
}while(index != string::npos);
//the last token
substring = str.substr(start);
dest.push_back(substring);
}
/**************************
*函数名:readFile
*函数功能:读取文件,VS平台下内存映射采用SDK的API
*参数说明:
*fileName:文件全名
*vecDoubleList:输出数据引用
**************************/
void readFile(const string &fileName,vector< vector<double> >&vecDoubleList)
{
HANDLE hFile = CreateFileA(fileName.c_str(),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
return ;
}
// Create a file-mapping object for the file.
HANDLE hFileMapping = CreateFileMappingA(hFile,NULL,PAGE_WRITECOPY,0, 0, NULL);
char *data = (char *)MapViewOfFile(hFileMapping, FILE_MAP_COPY, 0, 0, 0);
vector<string>vec;
vector< vector<string> >veclist;
split(data,"\n",vec);
for(int i = 0;i != vec.size();i++)
{
vector<string>tmp;
split(vec[i]," ",tmp);
veclist.push_back(tmp);
}
/*********************
vec.size()修改为veclist.size(), 否则会出现vector越界。同时,移除了除以10000.主要因为
本函数是一个读取文件通用函数,不是针对地面反射率数据,因此移除了除以10000,并把除以10000
放到主函数外面。
**********************/
for(int i = 0; i != veclist.size();i++)
{
vector<double>tmp;
for(int j = 0;j != veclist[0].size();j++)
{
tmp.push_back(atof(veclist[i][j].c_str()));//参数除以10000转换
}
vecDoubleList.push_back(tmp);
}
UnmapViewOfFile(data);
CloseHandle(hFileMapping);
CloseHandle(hFile);
}
阿神2017-04-17 11:33:31
你的代码是什么样的,能贴一下么,正常来说百兆/s是没问题的
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <cstdlib>
int main() {
std::ifstream is("test_file.txt");
int line_size = 500 * 3;
char line[line_size];
is.seekg(0, is.end);
int file_size = is.tellg();
is.seekg(0, is.beg);
int line_number = 1;
std::vector< std::vector<double> > result;
while(is.good()) {
is.getline(line, file_size);
if(strlen(line) == 0) {
continue;
}
std::stringstream ss;
ss << line;
char number_str[100];
std::vector<double> line_doubles;
while(ss.good()) {
ss.getline(number_str, line_size, ' ');
if(strlen(number_str) == 0) {
continue;
}
line_doubles.push_back(atof(number_str));
//std::cout << "get number:" << atof(number_str) << std::endl;
}
//std::cout << "get line_doubles size:" << line_doubles.size() << std::endl;
result.push_back(line_doubles);
//std::cout << "get line" << "(" << line_number++ <<"):" << line << std::endl;
}
//std::cout << "get result size:" << result.size() << std::endl;
is.close();
}
测试了下,大概是34ms