Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文本文件快速解析数据_C++_File_Parsing_Optimization - Fatal编程技术网

C++ 从文本文件快速解析数据

C++ 从文本文件快速解析数据,c++,file,parsing,optimization,C++,File,Parsing,Optimization,我把一个完整的文件读入一个字符串。这很快。例如180Mb文件-2s 然后,我使用>>操作符从字符串中提取一些值,并从中创建几个数组,将数组插入到结构中,然后将每个结构添加到向量中 我试图找到瓶颈,因为这是非常缓慢,但也许你不能做任何事情 >>方法快吗 string str; // gets filled with the file struct A; std::vector<A> b; // global variables // in the function inside the

我把一个完整的文件读入一个字符串。这很快。例如180Mb文件-2s 然后,我使用>>操作符从字符串中提取一些值,并从中创建几个数组,将数组插入到结构中,然后将每个结构添加到向量中

我试图找到瓶颈,因为这是非常缓慢,但也许你不能做任何事情

>>方法快吗

string str; // gets filled with the file
struct A;
std::vector<A> b; // global variables
// in the function inside the loop 
str >> a.val
A a;
b.push_back(a);
向量是拥有a的所有权还是复制?是否仍在堆栈上?我有大约60000个结构被插入到向量中。这是一种快速的方法还是有更好的方法


C/I/O常常比C++ I/O快。尝试用FSCANF解析数据块:你可能会发现C方法运行得快很多。

< P>问题是>方法快?< /P>
string str; // gets filled with the file
struct A;
std::vector<A> b; // global variables
// in the function inside the loop 
str >> a.val
A a;
b.push_back(a);
快速回答是相对的。你把它和什么比较

问题是向量是拥有a的所有权还是复制

Answer std::vector::push_back生成输入对象的副本

问题是堆栈上还有一个问题吗

答案仅根据张贴的代码判断,是的,A和b都在堆栈上

Queston我有大约60000个结构,可以插入到向量中。这是一种快速方法还是有更好的方法

答:通过创建具有所需大小的b并将数据直接读取到b中的对象中,您可能会获得一些性能

更新


如果您能够,以二进制形式写入和读取数据将是最快的。使用std::ostream::write写入数据,使用std::istream::read读取数据。

std::vector获取a的副本。不过,您可以使用std::vector::emplace避免这种情况。原则上,您的代码很好!否则你的问题太宽泛了,对不起!请参阅可用的。