look for congruence

  • Thread starter BjoernJackschina
  • Start date
B

BjoernJackschina

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
 
J

Jeff Schwab

BjoernJackschina said:
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. :(
 
D

David Harmon

On 8 May 2004 04:55:10 -0700 in comp.lang.c++, (e-mail address removed)
(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;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top