|
| 1 | +//sorts array of strings with user written comparison function |
| 2 | +#include<iostream> |
| 3 | +#include<string.h> |
| 4 | +#include<functional> |
| 5 | +#include<algorithm> |
| 6 | +using namespace std; |
| 7 | +char *names[] = {"George","Penny","Estelle","Don","Mike","Bob"}; |
| 8 | +bool alpha_comp(char *,char*); |
| 9 | +string names1[] = {"George","Penny","Estelle","Don","Mike","Bob"};//Input Strings Array For Sorting |
| 10 | +bool isDon(string name) |
| 11 | +{ |
| 12 | + return name=="Don"; |
| 13 | +} |
| 14 | +double in_to_cm(double s) |
| 15 | +{ |
| 16 | + return (s * 2.54); |
| 17 | +} |
| 18 | +int main() |
| 19 | +{ |
| 20 | + double centi[7]; |
| 21 | + double inches[] = {3.5,6.4,6,7,8};//Input Values In Inches For Converting To Centimetres |
| 22 | + transform(inches,inches+7,centi,in_to_cm); |
| 23 | + for(int i=0;i<7;i++) |
| 24 | + cout << centi[i] << "\t"; |
| 25 | + cout << endl; |
| 26 | + string *ptr; |
| 27 | + ptr=find_if(names1,names1+5,isDon); |
| 28 | + if(ptr==names1+5) |
| 29 | + cout << "Don is not on the list \n"; |
| 30 | + else |
| 31 | + { |
| 32 | + cout << "Don is element " << (ptr-names1) << " on the list\n"; |
| 33 | + } |
| 34 | + sort(names,names+6,alpha_comp); |
| 35 | + for(int i=0;i<6;i++) |
| 36 | + cout << names[i] << endl; |
| 37 | + return 0; |
| 38 | +} |
| 39 | +bool alpha_comp(char *s1,char *s2) |
| 40 | +{ |
| 41 | + return (strcmp(s1,s2)<0)?true:false; |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// Input: |
| 46 | +//sorts these string values |
| 47 | +// string names1[] = {"George","Penny","Estelle","Don","Mike","Bob"}; |
| 48 | +//converts these inches to centimetres |
| 49 | +// double inches[] = {3.5,6.4,6,7,8}; |
| 50 | + |
| 51 | +// Output: |
| 52 | +// 8.89 16.256 15.24 17.78 20.32 5.34025e-317 22.5806 |
| 53 | +// Don is element 3 on the list |
| 54 | +// Bob |
| 55 | +// Don |
| 56 | +// Estelle |
| 57 | +// George |
| 58 | +// Mike |
| 59 | +// Penny |
0 commit comments