Насколько большие файлы. Если вы можете держать их обоих в памяти,
это относительно просто при использовании STL:
std::vector<std::string> v(
(std::istream_iterator<std::string>( ifile1 )),
(std::istream_iterator<std::string>()));
v.insert(v.end(),
std::istream_iterator<std::string>( ifile2 ),
std::istream_iterator<std::string>());
std::sort( v.begin(), v.end() );
std::copy( v.begin(), std::unique( v.begin(), v.end() ),
std::ostream_iterator<std::string>( ofile, "\n" ) );
или
std::vector<std::string> v1(
(std::istream_iterator<std::string>( ifile1 )),
(std::istream_iterator<std::string>()) );
std::sort( v1.begin(), v1.end() );
v1.erase( std::unique( v1.begin(), v1.end() ), v1.end() );
std::vector<std::string> v2(
(std::istream_iterator<std::string>( ifile2 )),
(std::istream_iterator<std::string>()) );
std::sort( v2.begin(), v2.end() );
v2.erase( std::unique( v2.begin(), v2.end() ), v2.end() );
std::set_intersection( v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::ostream_iterator<std::string>( ofile, "\n" ) );
Если они не вписываются в память, вам, вероятно, придется отсортировать каждый
файл (используя system
для вызова вашей локальной утилиты), затем выполните
объединить вручную:
class FilterDuplicates
{
std::ostream& myDest;
std::string myLastOutput;
public:
Outputter( std::ostream& dest ) : myDest( dest ) {}
void write( std::string const& word ) const
{
if ( word != myLastOutput ) {
myDest << word;
myLastOutput = word;
}
}
};
ifile1 >> s1;
ifile2 >> s2;
FilterDuplicates out( ofile )
while ( ifile1 && ifile2 ) {
if ( s1 < s2 ) {
out.write( s1 );
ifile1 >> s1;
} else {
out.write( s2 );
ifile2 >> s2;
}
}
while ( ifile1 ) {
out.write( s1 );
ifile1 >> s1;
}
while ( ifile2 ) {
out.write( s2 );
ifile2 >> s2;
}