Created
September 9, 2015 02:11
-
-
Save N0taN3rd/bf62c34abbae63c0d164 to your computer and use it in GitHub Desktop.
struct operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
struct stringStruct{ | |
string stringMember; | |
bool operator<(stringStruct a){ | |
return stringMember < a.stringMember; | |
} | |
bool operator<(stringStruct* a){ | |
return stringMember < a->stringMember; | |
} | |
}; | |
ostream& operator<<(ostream& out,stringStruct& ss){ | |
return out<<ss.stringMember; | |
} | |
ostream& operator<<(ostream& out,stringStruct* ss){ | |
return out<<ss->stringMember; | |
} | |
struct sorter{ | |
bool operator()(stringStruct* a, stringStruct* b){ | |
return a < b; | |
} | |
}; | |
void sort1(stringStruct* sStructArray,int& size,sorter& sortr){ | |
sort(sStructArray,sStructArray+size,sortr); | |
} | |
void sort2( stringStruct* sStructArray,int size){ | |
std::sort(sStructArray,sStructArray+size,[](stringStruct a,stringStruct b){ | |
return a < b; | |
}); | |
} | |
template <int size> | |
void sort3(stringStruct* stringStructArray){ | |
sort(stringStructArray,stringStructArray+size,[](stringStruct a,stringStruct b){ | |
return a < b; | |
}); | |
} | |
int main() { | |
sorter sort; | |
stringStruct sStructArray[] = | |
{"G","H","D","E","F","I","U","V","W","J","K","L","A","T","X","Y","B","C","P","Q", "M","N","O","R","S","Z"}; | |
const int size = sizeof(sStructArray) / sizeof(stringStruct); | |
cout<<"sizeof array "<<sizeof(sStructArray)<<" sizeof struct "<<sizeof(stringStruct)<<" "<<size<<endl; | |
sort3<size>(sStructArray); | |
for(auto ss : sStructArray){ | |
cout<<ss<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment