一,c數組做爲參數退化的問題
1.c/c++沒有記錄數組的大小,因此用指針訪問數組中的元素的時候,我們要確保沒有超過數組的邊界,
通過下面的程序瞭解數組與指針的區別。
array.c:
#include <stdio.h>
#include <string.h>
int GetSize(int data[])
{
return sizeof(data);
}
int main()
{
int data1[] = {1, 2, 3, 4, 5};
/*data1是一個數組*/
int size1 = sizeof(data1);
int* data2 = data1;
/*data2是一個指針,經管他指向了數組data1的第一個數字,但他的本質任然是一個指針*/
int size2 = sizeof(data2);
/*在C/C++中,當數組做爲函數的參數進行傳遞時,數組自動退化爲同類型的指針*/
int size3 = GetSize(data1);
printf("%d, %d, %d\n", size1, size2, size3);
return 0;
}
Makefile:
.PHONY:clean
CC=gcc
CFLAGS=-Wall -g
BIN=test
OBJS=array.o
LIBS=
$(BIN):$(OBJS)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o $(BIN)
結果:
20, 4, 4
二,二維數組中的查找
a < 0; a = 0 ; a > 0;這個是微積分基本定理;這個很重要!!!
二維數組,每行每列都是遞增排序,給你個數字,判斷這個數字是不是這個二維數組當中的。
與數組的右上角的數字進行比較,如果大於這個右上角的數字,剔除這一行;如果小於這個右上角的數字,剔除這一列;
這樣子每一次比較範圍就減少了一行或者一列,知道找到最終的位置或者遍歷結束沒有找到這個數字;
其實我們也可以選取左下角的那個數字做爲比較的“軸”;
但是不能夠選擇左上角和右下角,這兩個位置不能夠每次遍歷消除一行或者一列;
DouArr.cpp:
#include <iostream>
#include <stdio.h>
using namespace std;
/*bool是標準C++數據類型,可取值true和false。單獨佔一個字節,*/
bool Find(int* matrix, int rows, int columns, int number)
{
bool found = false;
if(matrix != NULL && rows > 0 && columns > 0)
{
/*初始化第一次右上角的座標位置--記得在循環中更新這兩個座標*/
int row = 0;
int column = columns - 1;
/*循環的過程中row區域rows,column趨於0*/
while(row < rows && column >= 0)
{
/*C/C++二維數組是可以這麼搞的,因爲二維數組在內存中佔據連續的內存空間,千萬不要用java這麼搞*/
/*在內存中從上倒下存儲各行元素,在同一行中按照從左到右的順序存儲。根據行號和列好計算出相對於數組首地址的偏移量,從而找到對應元素*/
if (matrix[row * columns + column] == number)
{
found = true;
break;
}
else if(matrix[row * columns + column] > number)
{
-- column;
}
else
{
++ row;
}
}
}
return found;
}
// ====================測試代碼====================
void Test(char* testName, int* matrix, int rows, int columns, int number)
{
if(testName != NULL)
printf("%s begins: ", testName);
bool result = Find(matrix, rows, columns, number);
if(result == true)
{
cout<<"Find it!"<<endl;
}
else
{
cout<<"No find it!"<<endl;
}
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數在數組中
void Test1()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test1", (int*)matrix, 4, 4, 7);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數不在數組中
void Test2()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test2", (int*)matrix, 4, 4, 5);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數是數組中最小的數字
void Test3()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test3", (int*)matrix, 4, 4, 1);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數是數組中最大的數字
void Test4()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test4", (int*)matrix, 4, 4, 15);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數比數組中最小的數字還小
void Test5()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test5", (int*)matrix, 4, 4, 0);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的數比數組中最大的數字還大
void Test6()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test6", (int*)matrix, 4, 4, 16);
}
// 魯棒性測試,輸入空指針
void Test7()
{
Test("Test7", NULL, 0, 0, 16);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
return 0;
}
Makefile:
.PHONY:clean
CPP=g++
CFLAGS=-Wall -g
BIN=test
OBJS=DouArr.o
LIBS=
$(BIN):$(OBJS)
$(CPP) $(CFLAGS) $^ -o $@ $(LIBS)
%.o:%.cpp
$(CPP) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o $(BIN)
運行結果:
Test1 begins: Find it!
Test2 begins: No find it!
Test3 begins: Find it!
Test4 begins: Find it!
Test5 begins: No find it!
Test6 begins: No find it!
Test7 begins: No find it!