C++ 合并两个单词表
我想把两个单词表合并成一个文件。必须删除所有重复项。每个单词用一个换行符隔开。我搜索过这种程序,但什么也找不到。我在寻找正确的东西吗?是否有此功能的c/c++实现?C++ 合并两个单词表,c++,c,merge,C++,C,Merge,我想把两个单词表合并成一个文件。必须删除所有重复项。每个单词用一个换行符隔开。我搜索过这种程序,但什么也找不到。我在寻找正确的东西吗?是否有此功能的c/c++实现?//读取输入 // read input std::ifstream in( file_path ); typedef std::set< std::string > wordlist_type; wordlist_type wordlist; std::string word; while ( in >>
//读取输入
// read input
std::ifstream in( file_path );
typedef std::set< std::string > wordlist_type;
wordlist_type wordlist;
std::string word;
while ( in >> word ) {
wordlist.insert( word );
}
// repeat with other files to merge more wordlists
// now output to a new file
std::ofstream out( output_path );
for ( wordlist_type::iterator it = wordlist.begin(); it != wordlist.end(); ++ it ) {
out << * it << '\n';
}
std::ifstream-in(文件路径);
typedef std::setwordlist\u type;
字表\类型字表;
字符串字;
while(在>>word中){
单词表。插入(单词);
}
//对其他文件重复此操作以合并更多单词列表
//现在输出到一个新文件
std::流输出(输出路径);
for(wordlist_type::iterator it=wordlist.begin();it!=wordlist.end();+it){
out//读取输入
std::ifstream-in(文件路径);
typedef std::setwordlist\u type;
字表\类型字表;
字符串字;
while(在>>word中){
单词表。插入(单词);
}
//对其他文件重复此操作以合并更多单词列表
//现在输出到一个新文件
std::流输出(输出路径);
for(wordlist_type::iterator it=wordlist.begin();it!=wordlist.end();+it){
如果您有权访问unix,请退出
cat file1 file2 | sort | uniq > file3
如果您有权访问unix
cat file1 file2 | sort | uniq > file3
像这样的
std::set<std::string> words;
std::string word;
while(cin >> word)
if (words.insert(word).second)
cout << word;
std::设置单词;
字符串字;
while(cin>>word)
如果(字。插入(字)。秒)
像这样的事情
std::set<std::string> words;
std::string word;
while(cin >> word)
if (words.insert(word).second)
cout << word;
std::设置单词;
字符串字;
while(cin>>word)
如果(字。插入(字)。秒)
cout这些文件有多大。如果你能把它们都保存在内存中,
使用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" ) );
这些文件有多大如果你能把它们都保存在内存中,
使用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" ) );
cat-wordlist1-wordlist2 | sort-u>onefile
或更好的“sort-u-wordlist1-wordlist2>onefile”@Chris:是的,更好——但我的目标是:)cat-wordlist1-wordlist2 | sort-u>onefile
或更好的“sort-u-wordlist1-wordlist2>onefile”@克里斯:是的,更好——但我的目标是:)-1:不是答案。不消除重复:它只消除连续的重复。-1:不是答案。也不消除重复:它只消除连续的重复。如果你需要使用Unix工具包:排序-u input1 input2>输出
trick立即:-)(我认为是Windows,因为他试图编写一个程序来实现它,在Unix下这是不必要的。)@詹姆斯:甚至Windoze也有类型和管道-我认为它只是在管理这种复杂程度…Windows排序实用程序是否具有与Unix-u
选项等效的功能?或者Windows是否具有与Unix唯一的
等效的功能?@James:Windows XP排序
没有任何-u
等效功能,一个我找不到任何唯一的等价物,但他们可能会称之为晦涩难懂的东西。如果您需要使用Unix工具包:sort-u input1 input2>output
立即执行此操作:-)。(我有点像Windows,因为他试图编写一个程序来执行此操作,这在Unix下是不必要的。)@詹姆斯:甚至Windoze也有类型和管道-我认为它只是在管理这种复杂程度…Windows排序实用程序是否具有与Unix-u
选项等效的功能?或者Windows是否具有与Unix唯一的
等效的功能?@James:Windows XP排序
没有任何-u
等效功能,一个我找不到任何独特的等价物,但他们可能会称之为晦涩难懂的东西。
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;
}