By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

look for congruence

P: n/a
Hello,
I want to sort several words in a textfile in view of alphabet like
this:
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"));

After that I want to read this new file and look for congruence. What
is a capability to compare all this word-orders?
Many thanks for reply.
Bjoern
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
BjoernJackschina wrote:
Hello,
I want to sort several words in a textfile in view of alphabet like
this:
stop is opst
manage is aaegmn.
I think it will work with: #include <algorithm>

sort (word.begin(),word.end());
Either you've included <algorithm> inside function body, or you've tried
to perform the sort outside a function body. Either way, this isn't
what you want to do. Also, how do you define "congruence?" The
std::sort algorithm sorts lexicographically by default.
system(sort("file.doc > newfile.doc"));
/* ITYM: */ system( "sort file.doc > newfile.doc" );
After that I want to read this new file and look for congruence. What
is a capability to compare all this word-orders?
Sorry, not sure what you mean. :(
Many thanks for reply.
Bjoern

Jul 22 '05 #2

P: n/a
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;
}
}
}

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.