1: using namespace std; #include "vector" template <typename T> void DecreaseSort(std::vector<T>& coll) { typename vector<T>::iterator iter; for(iter = coll.begin();iter != coll.end(); iter++) { typename vector<T>::iterator iterLower = iter+1; for(;iterLower!=coll.end();iterLower++) { if(*iter<*iterLower) { *iter = *iter + *iterLower; *iterLower = *iter - *iterLower; *iter = *iter - *iterLower; } } } } 2:改进 void DecreaseSort(std::vector<T>& coll) { typename vector<T>::iterator iter; for(iter = coll.begin();iter != coll.end(); iter++) { typename vector<T>::iterator iterLower = iter; typename vector<T>::iterator iterSmall = iterLower; for(;iterLower!=coll.end();iterLower++) { if(*iterSmall<*iterLower) { iterSmall = iterLower; } } if(iterSmall!=iter) { *iter = *iter + *iterSmall; *iterSmall = *iter - *iterSmall; *iter = *iter - *iterSmall; } } } 3:改进 void DecreaseSort(std::vector<T>& coll) { typename vector<T>::iterator iter; for(iter = coll.begin();iter != coll.end(); iter++) { typename vector<T>::iterator iterLower = iter+1; typename vector<T>::iterator iterSmall = iter; for(;iterLower!=coll.end();iterLower++) { if(*iterSmall<*iterLower) { iterSmall = iterLower; } } if(iterSmall!=iter) { *iter = *iter + *iterSmall; *iterSmall = *iter - *iterSmall; *iter = *iter - *iterSmall; } } }