Remove anagrams from a string vector

I am trying to make a program which removes all anagrams from vector. I sorted them alphabetical and i put them in a class, hopefully it would be easier to delete de anagrams, but it didnt acctualy helped.

#include <iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct Fraza{
    string word;
    string word_alph;
    Fraza(){};
    Fraza(string words,string word_alph){
        this->word=move(word);
        this->word_alph=move(word_alph);
    }
    void printverify() const{
        cout<<word<<" is sorted "<<word_alph<<endl;
    }
    void print() const{
        cout<<word<<endl;
    }


};

int main() {
    Fraza fraza [100];
    vector<Fraza>text;
    string tmp;
    string tmpa;
// i am taking the input
    while(cin>>tmp){
        tmpa=tmp;
        sort(tmpa.begin(),tmpa.end());
        text.emplace_back(Fraza(tmp,tmpa));

    }
// trying to delete the words

for(int i=0;i<text.size();i++){
    for(int j=i+1;j<text.size();j++) {
        if (fraza[i].word_alph==fraza[j].word_alph) {
            text.erase(text.begin()+i);
            text.erase(text.begin()+j);
        }
    }
}
    for(const auto& it:text)
        it.printverify();
    for(const auto& it:text)
        it.print();


    return 0;
}

so far i know that it might work comparing the sorted word. If 2 words are the same, i should try delete the words on the specific position



Read more here: https://stackoverflow.com/questions/66999201/remove-anagrams-from-a-string-vector

Content Attribution

This content was originally published by Stefan Daniel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: