【发布时间】:2016-01-23 10:38:25
【问题描述】:
这可能是一个非常愚蠢的问题,但是我如何优化这段代码以使其更高效(更快,更少的内存消耗)?我制作了这段代码来帮助我对一些文本文件进行排序。它从第一个文件中读取每个字符串,然后搜索第二个文件,直到找到所有相关的字符串,然后在第三个文件中写入一些匹配的字符串。代码如下:
ifstream h("SecondFile.txt");
ifstream h2("FirstFile.txt");
ifstream uh("MatchedStrings.txt");
ofstream g("sorted.txt");
int main()
{
string x, y, z;
cout << "Sorting..." << endl;;
while (!h.eof()){
h >> x;
while (!h2.eof() || (y == x)){
h2 >> y;
uh >> z;
if (y == x){
g << z << endl;
break;
h2.clear();
h2.seekg(0);
uh.clear();
uh.seekg(0);
}
}
if (h2.eof() && (y != x)){
g << "none" << endl;
h2.clear();
h2.seekg(0);
uh.clear();
uh.seekg(0);
}
}
cout << "Finished!";
}
我已将代码更改为:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream h("SecondFile.txt");
ifstream h2("FirstFile.txt");
ifstream uh("MatchedStrings.txt");
ofstream g("sorted.txt");
int main()
{
string x;
bool write_none = true;
int i = 0,l=0;
string check[] = {""};
string unhashed_checked[] = { "" };
string sorted_array[] = { "" };
cout << "Sorting..." << endl;
//Get to memory
while (!h2.eof())
{
h2 >> check[i];
uh >> unhashed_checked[i];
i++;
}
while (!h.eof()){
h >> x;
write_none = true;
for (int t = 0; t <= i;t++)
{
if (x == check[t])
{
break;
write_none = false;
sorted_array[l] = unhashed_checked[i];
l++;
}
}
if (write_none)
{
sorted_array[l] = "none";
l++;
}
}
for (int k = 0; k <= l; k++)
{
g << sorted_array[k]<<endl;
}
cout << "Finished!";
}
但是我在运行程序时遇到了这个异常:
Unhandled exception at 0x01068FF6 in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC
【问题讨论】:
-
从内存中的第一个文件中收集所有搜索字符串并在外部循环中使用这些搜索第二个文件可能会更快。
-
我会尝试并返回结果。
-
@πάνταῥεῖ 我无法让它工作:/ 我尝试将它读入内存,但得到一个未处理的异常 0xccccccc
-
照@Ilya 说的,使用
std::vector<std::string>。 -
为什么您的所有流都是全球性的?无论如何,您只有一个功能!此外,没有这些文件,就不可能重现该问题。此外,文件中的输入是否甚至是重现问题所必需的?尝试先提取一个最小但完整的示例!
标签: c++ sorting search optimization text