滴水-day33-模板
使用模版实现swap(x, y)函数,功能:交换x, y的值.
template<class T, class E>
void swapA(T &x, E &y)
{
T temp = x;
x = y;
y = temp;
}
int main()
{
int x = 1;
short y = 44;
swapA(x, y);
cout << x << " " << y << endl;
return 0;
}
冒泡排序:对结构体或者类进行排序,如果不能实现,找出问题所在.
class Student
{
public:
int x;
int y;
public:
Student();
~Student();
private:
};
Student::Student()
{
this->x = 10;
this->y = 20;
}
Student::~Student()
{
cout << "析构函数执行" << endl;
}
template<class T>
void shorMao(T &array, int length)
{
int k = 0;
int j = 0;
for (k = 0; k < length - 1; k++)
{
for (j = 0; j < length - k - 1; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
Student stu1;
Student stu2;
Student stu3;
Student stu[] = { stu1 , stu2, stu3 };
shorMao(stu, 3);
其中在if比较处会出现问题,因为stu的元素是一个结构体,> 符号不知道如何比较两个结构体的大小
int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp;
这三行代码也有问题,首先就是赋值问题,结构体如何给int类型赋值呢,这显然不是可能的。
综上所诉,如果想要实现结构体或者类的排序,首先要定义一个比较规则,去告述编译器该如何比较两个类的大小,其次就是赋值问题,类该如何给int类型赋值呢。
观察下面两个Sort方法的反汇编代码(看内存地址和内容):
int arr[] = { 2,6,1,5,4 };
char arr1[] = { 2,6,1,5,4 };
shorMao(arr, 5);
shorMao(arr1, 5);
00415900 55 push ebp
00415901 8B EC mov ebp,esp
00415903 81 EC E4 00 00 00 sub esp,0E4h
00415909 53 push ebx
0041590A 56 push esi
0041590B 57 push edi
0041590C 8D 7D DC lea edi,[ebp-24h]
0041590F B9 09 00 00 00 mov ecx,9
00415914 B8 CC CC CC CC mov eax,0CCCCCCCCh
00415919 F3 AB rep stos dword ptr es:[edi]
0041591B B9 F1 D1 42 00 mov ecx,offset _BE50F95F_my_day33@cpp (042D1F1h)
00415920 E8 72 BB FF FF call @__CheckForDebuggerJustMyCode@4 (0411497h)
00415925 C7 45 F8 00 00 00 00 mov dword ptr [k],0
0041592C C7 45 EC 00 00 00 00 mov dword ptr [j],0
00415933 C7 45 F8 00 00 00 00 mov dword ptr [k],0
0041593A EB 09 jmp __$EncStackInitStart+39h (0415945h)
0041593C 8B 45 F8 mov eax,dword ptr [k]
0041593F 83 C0 01 add eax,1
00415942 89 45 F8 mov dword ptr [k],eax
00415945 8B 45 0C mov eax,dword ptr [length]
00415948 83 E8 01 sub eax,1
0041594B 39 45 F8 cmp dword ptr [k],eax
0041594E 7D 65 jge __$EncStackInitStart+0A9h (04159B5h)
00415950 C7 45 EC 00 00 00 00 mov dword ptr [j],0
00415957 EB 09 jmp __$EncStackInitStart+56h (0415962h)
00415959 8B 45 EC mov eax,dword ptr [j]
0041595C 83 C0 01 add eax,1
0041595F 89 45 EC mov dword ptr [j],eax
00415962 8B 45 0C mov eax,dword ptr [length]
00415965 2B 45 F8 sub eax,dword ptr [k]
00415968 83 E8 01 sub eax,1
0041596B 39 45 EC cmp dword ptr [j],eax
0041596E 7D 43 jge __$EncStackInitStart+0A7h (04159B3h)
00415970 8B 45 EC mov eax,dword ptr [j]
00415973 8B 4D 08 mov ecx,dword ptr [array]
00415976 8B 55 EC mov edx,dword ptr [j]
00415979 8B 75 08 mov esi,dword ptr [array]
0041597C 8B 04 81 mov eax,dword ptr [ecx+eax*4]
0041597F 3B 44 96 04 cmp eax,dword ptr [esi+edx*4+4]
00415983 7E 2C jle __$EncStackInitStart+0A5h (04159B1h)
00415985 8B 45 EC mov eax,dword ptr [j]
00415988 8B 4D 08 mov ecx,dword ptr [array]
0041598B 8B 14 81 mov edx,dword ptr [ecx+eax*4]
0041598E 89 55 E0 mov dword ptr [ebp-20h],edx
00415991 8B 45 EC mov eax,dword ptr [j]
00415994 8B 4D 08 mov ecx,dword ptr [array]
00415997 8B 55 EC mov edx,dword ptr [j]
0041599A 8B 75 08 mov esi,dword ptr [array]
0041599D 8B 54 96 04 mov edx,dword ptr [esi+edx*4+4]
004159A1 89 14 81 mov dword ptr [ecx+eax*4],edx
004159A4 8B 45 EC mov eax,dword ptr [j]
004159A7 8B 4D 08 mov ecx,dword ptr [array]
004159AA 8B 55 E0 mov edx,dword ptr [ebp-20h]
004159AD 89 54 81 04 mov dword ptr [ecx+eax*4+4],edx
004159B1 EB A6 jmp __$EncStackInitStart+4Dh (0415959h)
004159B3 EB 87 jmp __$EncStackInitStart+30h (041593Ch)
004159B5 5F pop edi
004159B6 5E pop esi
004159B7 5B pop ebx
004159B8 81 C4 E4 00 00 00 add esp,0E4h
004159BE 3B EC cmp ebp,esp
004159C0 E8 92 B9 FF FF call __RTC_CheckEsp (0411357h)
004159C5 8B E5 mov esp,ebp
004159C7 5D pop ebp
004159C8 C3 ret
004143B0 55 push ebp
004143B1 8B EC mov ebp,esp
004143B3 81 EC E4 00 00 00 sub esp,0E4h
004143B9 53 push ebx
004143BA 56 push esi
004143BB 57 push edi
004143BC 8D 7D DC lea edi,[ebp-24h]
004143BF B9 09 00 00 00 mov ecx,9
004143C4 B8 CC CC CC CC mov eax,0CCCCCCCCh
004143C9 F3 AB rep stos dword ptr es:[edi]
004143CB B9 F1 D1 42 00 mov ecx,offset _BE50F95F_my_day33@cpp (042D1F1h)
004143D0 E8 C2 D0 FF FF call @__CheckForDebuggerJustMyCode@4 (0411497h)
004143D5 C7 45 F8 00 00 00 00 mov dword ptr [k],0
004143DC C7 45 EC 00 00 00 00 mov dword ptr [j],0
004143E3 C7 45 F8 00 00 00 00 mov dword ptr [k],0
004143EA EB 09 jmp __$EncStackInitStart+39h (04143F5h)
004143EC 8B 45 F8 mov eax,dword ptr [k]
004143EF 83 C0 01 add eax,1
004143F2 89 45 F8 mov dword ptr [k],eax
004143F5 8B 45 0C mov eax,dword ptr [length]
004143F8 83 E8 01 sub eax,1
004143FB 39 45 F8 cmp dword ptr [k],eax
004143FE 7D 64 jge __$EncStackInitStart+0A8h (0414464h)
00414400 C7 45 EC 00 00 00 00 mov dword ptr [j],0
00414407 EB 09 jmp __$EncStackInitStart+56h (0414412h)
00414409 8B 45 EC mov eax,dword ptr [j]
0041440C 83 C0 01 add eax,1
0041440F 89 45 EC mov dword ptr [j],eax
00414412 8B 45 0C mov eax,dword ptr [length]
00414415 2B 45 F8 sub eax,dword ptr [k]
00414418 83 E8 01 sub eax,1
0041441B 39 45 EC cmp dword ptr [j],eax
0041441E 7D 42 jge __$EncStackInitStart+0A6h (0414462h)
00414420 8B 45 08 mov eax,dword ptr [array]
00414423 03 45 EC add eax,dword ptr [j]
00414426 0F BE 08 movsx ecx,byte ptr [eax]
00414429 8B 55 08 mov edx,dword ptr [array]
0041442C 03 55 EC add edx,dword ptr [j]
0041442F 0F BE 42 01 movsx eax,byte ptr [edx+1]
00414433 3B C8 cmp ecx,eax
00414435 7E 29 jle __$EncStackInitStart+0A4h (0414460h)
00414437 8B 45 08 mov eax,dword ptr [array]
0041443A 03 45 EC add eax,dword ptr [j]
0041443D 0F BE 08 movsx ecx,byte ptr [eax]
00414440 89 4D E0 mov dword ptr [ebp-20h],ecx
00414443 8B 45 08 mov eax,dword ptr [array]
00414446 03 45 EC add eax,dword ptr [j]
00414449 8B 4D 08 mov ecx,dword ptr [array]
0041444C 03 4D EC add ecx,dword ptr [j]
0041444F 8A 51 01 mov dl,byte ptr [ecx+1]
00414452 88 10 mov byte ptr [eax],dl
00414454 8B 45 08 mov eax,dword ptr [array]
00414457 03 45 EC add eax,dword ptr [j]
0041445A 8A 4D E0 mov cl,byte ptr [ebp-20h]
0041445D 88 48 01 mov byte ptr [eax+1],cl
00414460 EB A7 jmp __$EncStackInitStart+4Dh (0414409h)
00414462 EB 88 jmp __$EncStackInitStart+30h (04143ECh)
00414464 5F pop edi
00414465 5E pop esi
00414466 5B pop ebx
00414467 81 C4 E4 00 00 00 add esp,0E4h
0041446D 3B EC cmp ebp,esp
0041446F E8 E3 CE FF FF call __RTC_CheckEsp (0411357h)
00414474 8B E5 mov esp,ebp
00414476 5D pop ebp
00414477 C3 ret
这两个不同类型的数组,在排序的代码中汇编代码相差无几,就是一些取值不在是DWORD类型而是BYTE类型
二分查找
int FindValues(int array[], int length, int FindValue)
{
int strat = 0;
int end = length - 1;
while (strat <= end)
{
int temp = (strat + end) / 2;
if (array[temp] > FindValue)
{
end = temp - 1;
}
else if (array[temp] < FindValue)
{
strat = temp + 1;
}
else {
return temp;
}
}
return -1;
}
二分查找适用于数组已排序完毕,
License:
CC BY 4.0