I'm trying to sort a vector< pair<int,char> >
but i want to change the behavior of the comparison operators of the pair type , so that if the first values equal and it'a comparing with (>) operator, i want it to compare the second value with (<) operator.
I'm trying to do this to solve the "What's Cryptanalysis?" problem on uva . Here is my approach :
string toLower(string in){
string out;
for(int i=0;i<in.length();i++){
if(in.at(i)<='Z' && in.at(i)>='A'){
out+=in.at(i)+('a'-'A');
}
else if(in.at(i)<='z' && in.at(i)>='a'){
out+=in.at(i);
}
}
return out;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("tmp.txt","w",stdout);
vector< pair<int,char> >vp;
pair<int,char> tp;
for(char a='a';a<='z';a++){//buliding a table of values and chars
tp= make_pair(0,a);
vp.push_back(tp);
}
int T;
cin >> T;
string s;
cin.ignore();
for(int i=0;i<T;i++){
getline(cin,s);
s=toLower(s);//remove special chars and convert all to lower
int l=s.length();
for(int j=0;j<l;j++){
vp[s[j]-'a'].first+=1;//increasing the value of each char found
}
}
sort(vp.begin(),vp.end());//ascending sort
for(int j=25;j>=0;j--){
cout << (char)(vp[j].second -('a'-'A')) << " " <<vp[j].first << endl;//cout the Capital char and its value backwards (Descending)
}
//system("pause");
return 0;
}
But this is how output looks like:
S 7
T 6
I 5
E 4
O 3
W 2
U 2
N 2
H 2
A 2
Y 1
Q 1
M 1
C 1
Z 0
X 0
V 0
R 0
P 0
L 0
K 0
J 0
G 0
F 0
D 0
B 0
so for example i want W U N H A
to be A H N U W
i have read about overloading in other questions but i don't know to implement it here