自考新教材-p334

源程序:

#include <iostream>
#include <string>
using namespace std;
template <typename T>
int myCompare(const T& left, const T& right)
{
if (left<right)
{
return-1;
}
else if (right<left)
{
return 1;
}
else return 0;
}
template<class T>
void Swap(T&x, T&y)
{
T tmp = x;
x = y;
y = tmp;
}
int main()
{
string arraystring[10] = { "shang","xia","zuo","you","qian","hou","dong","xi","nan","bei" };
int j;
string temp;
for (int i = 1; i<10; i++)
{
j = i;
while (j>0 && myCompare<string>(arraystring[j - 1], arraystring[j])>0)
{
swap(arraystring[j], arraystring[j - 1]);
j--;
}
}
for (int i = 0; i < 10; i++)
cout << arraystring[i] << endl;
system("pause");
return 1;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12266458.html