一衣吠、數組相關函數
int num[10] = {5,2,3,3,4,8,1}; //用戶設置的初始長度會被忽略
printf('%s\n',num);
printf('=================================\n');
resize(num,10); //設置數組的長度期虾,變長毁习,新值為0或''
printf('%s\n',num);//注意這類function不返回數據根竿,而直接修改所傳入的數組變量
printf('=================================\n');
resize(num,6); //設置數組的長度迄薄,變短疫诽,當然會刪去已有元素
printf('%s\n',num);
printf('=================================\n');
string word[] = {'$','HIP','/'}; //創(chuàng)建字符串數組
printf('%s\n',word );
printf('%s\n',len(word) ); //len(array) 返回數組的長度(元素數量)
printf('%s\n',len(word[1]) ); //len(string) 返回字符的數量
printf('=================================\n');
string last = pop(word); //pop() 將數組最后一個元素抽去并返回钻心,數組長度減一
int numm[] = {5,2,3,3,4,8,1};
printf('%s\n',last );
printf('%s\n',word );
pop(word,0); //依據id將指定元素抽離
printf('%s\n',word );
pop(numm,-2); //刪去倒數第二個
printf('%s\n',numm );
//removeindex(&array[], index)有相同效果
int ture = removevalue(numm,3); //刪除數組中發(fā)現(xiàn)的第一個數值為3的元素唉匾,
printf('%s\n',ture ); //刪除成功返回1孕讳,否則返回0
printf('%s\n',numm );
printf('=================================\n');
append(word,'/geo'); //在數組的末尾添加一個元素,相同功能的還有push()
word[5] = '/agents';
append(word, {'/zombie','/clip'} ); //在數組的末尾拼接另一個數組(literal)
string test[] = {'/walk'};
append(word,test); //在數組的末尾拼接另一個數組(variable)
printf('%s\n',word );
printf('=================================\n');
int pts[] = array(1, 2.5, 'k', {5,4,3} ); //用 各種數據和變量 創(chuàng)建一個數組
printf('%s\n',pts ); //都會被轉成指定類型巍膘,這里是整數
printf('=================================\n');
vector pos[] = { {1,2,3}, {4,5,6} };
matrix3 rot[] = { {{1,0,0}, {0,1,0}, {0,0,1}} , {{0,0,2}, {2,0,0}, {0,2,0}} };
float posfloat[] = serialize(pos); //將向量數組 展平 成浮點數組
float rotfloat[] = serialize(rot); //將矩陣數組 展平 厂财,每個分量都是一個浮點元素
printf('%s\n',posfloat );
printf('%s\n',rotfloat );
printf('=================================\n');
vector vel[];
matrix3 scale[];
float flt[] = { 1, 1, 0, 1, 2, 0, 3, 1, 3 };
vel = unserialize(flt); //將浮點數組 組合 成向量數組,3個浮點一個向量
scale = unserialize(flt);//將浮點數組 組合 成矩陣數組
printf('%s\n',vel );
printf('%s\n',scale );
printf('=================================\n');
int number[] = { 5, 2, 3, 1, 4, 6, 7, 9, 0 };
printf('%s\n',min(number) );
printf('%s\n',max(number) );
printf('%s\n',avg(number) );
printf('%s\n',sort(number) );//返回新的排好序的數組峡懈,排序方式從小到大璃饱,原數組不變
printf('%s\n',argsort(number) );//返回數組,用這個數組的每一個值去給原數組排序肪康,就是從小到大的
int sorted[];
for(int i=0; i<len(number); ++i)
{
int temp[] = argsort(number);
sorted[i] = number[temp[i]];
}
printf('%s\n',sorted );
float re[] = reorder(flt,argsort(flt));
printf('%s\n',re );
reverse(sorted);//返回新的數組荚恶,反轉順序,原數組不變磷支,還可以用來反轉字符串"hello" -> "olleh"
printf('%s\n',reverse(sorted) );
printf('=================================\n');
int digit[] = { 5, 2, 3, 1, 4, 6, 7, 9, 0 };
insert(digit,1,23); //在第n 號 元素前面插入元素
insert(digit,-1,43); //在倒數第n 個 元素前面插入元素
printf('%s\n',digit );
int a[] = {8,8,8};
insert(digit,3,a); //插入數組變量
printf('%s\n',digit );
printf('=================================\n');
int data[] = { 5, 2, 3, 1, 4, 6, 7, 9, 0 };
printf('%s\n', isvalidindex(data,-10) );
//該數組序號范圍0~8或 -1 ~ -9(倒數第一個~倒數第九個)
//如果序號在這數組中有谒撼,isvalidindex()返回真,否則返回假
//等價于index < len(array) && index >= -len(array)
// 這個例子中 id<9 && id>=-9
printf('=================================\n');
//find() 回看自制neighbours函數視頻