find anagrams from given set of inputs

http://ideone.com/F3VYvO

This is my code.
I am not able to figure out what should I do to maintain a backup of my input and then map it with the changed array to display the result correctly
There is probably a way to do it using the map container, but I am not familiar with it. I would just create a class or struct container two string and create a vector from that instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

struct WordPair
{
    string original;
    string sorted;
    WordPair(string s) : original(s), sorted(s)
    {
        sort(sorted.begin(), sorted.end());
    }
};

bool WPCompare(const WordPair &lhs, const WordPair &rhs)
{
	return lhs.sorted < rhs.sorted;
}

int main()
{
    int t;
    cin>>t;
    string resp;
    vector<WordPair>words;
    while(t--)
    {
        cin>>resp;
        words.push_back(WordPair(resp));
    }
    sort(words.begin(),words.end(), WPCompare);
    for(int i=0; i<words.size()-1;i++)
        {
        if(words[i].sorted==words[i+1].sorted)
        {
            cout<<"ANAGRAM:";
            cout<<words[i].original<<"  "<<words[i+1].original<<endl;
        }
    }
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.