On 8 May 2004 04:55:10 -0700 in comp.lang.c++,
ja*******@hotmail.com
(BjoernJackschina) wrote,
stop is opst
manage is aaegmn.
I think it will work with:
#include <algorithm>
sort (word.begin(),word.end());
system(sort("file.doc > newfile.doc"));
You have not mentioned how the sorted anagrams will get back out to any
file. Is your data too big to hold in memory? Please compare what you
are doing with the std::map approach someone suggested earlier. Perhaps
you need to describe your requirements for us again?
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
string word;
map<string, string> wordmap;
ifstream input("file.txt");
while (input >> word) {
string anag = word;
sort (anag.begin(), anag.end());
if (wordmap.find(anag) != wordmap.end()) {
if (word != wordmap[anag])
cout << wordmap[anag] << "\tis\t" << word << '\n';
} else {
wordmap[anag] = word;
}
}
}