نوشته شده توسط
mr.keeper01
کسی جواب سوالمو نمیده بابا یکی جواب بده دیگه
اینطوری هم می تونید سورت کنید.!
الگوریتم شل بهینه شده درجی البته بدون عملیات تقسیم بندی...
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <memory.h>
#include <bits/unique_ptr.h>
using namespace std ;
struct MyStructs
{
static void destory(MyStructs* my){
delete my ;
}
static unique_ptr<MyStructs , decltype(&destory)> create(int k, string& s){
return unique_ptr<MyStructs , decltype(&destory)>(new MyStructs(k,s) , &destory);
}
int key;
string stringValue;
MyStructs(int k, const string& s) : key(k), stringValue(s) {}
private :
MyStructs(){}
~MyStructs(){}
};
void customSort(vector<MyStructs*>& vect){
for(vector<MyStructs*>::const_iterator ci=vect.cbegin(); ci !=vect.cend(); ++ci){
for(vector<MyStructs*>::const_iterator ji=ci; ji !=vect.cend(); ++ji){
if(vect.size()>0 && (*ji)->stringValue.compare((*ci)->stringValue) < 0){
auto tmp = MyStructs::create((*ci)->stringValue.size(),(*ci)->stringValue);
(*ci)->stringValue = (*ji)->stringValue;
(*ji)->stringValue = (*tmp.get()).stringValue;
(*ci)->key = (*ji)->key;
(*ji)->key = (*tmp.get()).key;
}
}
}
}
int main() {
vector<MyStructs*> vecStr ;
string vstr ;
char ch;
do{
cin >> vstr ;
auto myStr = new MyStructs(vstr.size(),vstr);
vecStr.push_back(myStr);
cout << "Enter another(y/n)?";
cin >> ch;
cin.ignore (numeric_limits<streamsize>::max (),'\n');
}while(ch == 'y' || ch =='Y');
cout << endl << endl ;
cout << "before sort vector collection..." << endl ;
for_each(vecStr.begin(), vecStr.end(),[](MyStructs* str) {
std::cout<<(*str).stringValue << " , " ;
});
customSort(vecStr);
cout << endl << endl << endl ;
cout << "after sort vector collection..." << endl ;
for(vector<MyStructs*>::const_iterator ci=vecStr.cbegin (); ci !=vecStr.cend (); ++ci){
cout << "vector element -> " << (*ci)->stringValue << endl ;
}
return 0;
}
لازم به توضیح بود بگید...