新手小白,本代码仅能实现功能,但是执行效率太低,希望有优化方法的大佬给予指点,不胜感激,谢谢!同时感谢陌生人的帮助。
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream myfile1("1.txt");
ifstream myfile2("2.txt");
ofstream outfile("out.txt", ios::trunc);
string str1[1024], str2[1024];
for (int j = 0; j < 1024; j++)
{
myfile1 >> str1[j];
}
for (int i= 0; i< 1024; i++)
{
myfile2>> str2[i];
}
for (int j = 0; j <1024; j++)
{
for (int i = 0; i < 1024; i++)
{
if (str2[i] == str1[j])
{
outfile <<str1[j] << endl;
}
}
};
myfile1.close();
myfile2.close();
outfile.close();
return 0;
}