第9周
9.1 指向指針的指針
T ** p:p是指向指針的指針,p指向的地方存放著一個類型為T*的指針,*p的類型是T*。
9.2 指針和字符串
字符串常量的類型是char*,字符數組名的類型也是char*。
9.3 字符串庫函數
int atoi(char* s); 將字符串s里的內容轉換成一個整型數返回。如果s的格式不是整數,則返回0。
double atof(char* s); 將字符串s中的內容轉換成實數返回。如果格式不為實數,則返回0。
char* itoa(int value, char* string, int radix); 將整型值value以radix進制表示法寫入string。
9.4 void指針和內存操作函數
void* p 可以用任何類型的指針對void指針進行賦值或初始化。
void* memset(void* dest, int ch, int n); 將從dest開始的n個字節,都設置成ch,返回值是dest。ch只有最低的字節起作用。
eg. char szname[100] = ""; memset(szname, 'a', 10);
用memset函數將數組內容全部設置成0:int a[100]; memset(a, 0, sizeof(a));
void* memcpy(void* dest, void& src, int n); 將地址src開始的n個字節,拷貝到地址dest,返回值是dest。
eg. int a1[10]; int a2[10]; memcpy(a2, a1, 10*sizeof(int));
9.5 函數指針
每個函數都會占用一段連續的內存空間,函數名就是該函數所占內存區域的起始地址(也稱入口地址)。將函數入口地址賦給一個指針變量,稱該指針為函數指針。
類型名 (* 指針變量名)(參數類型1, 參數類型2...) eg. int(*pf)(int, char) pf是一個函數指針,所指向函數的返回值為int,該函數有兩個參數,分別是int和char。
可以用一個原型匹配的函數名給一個函數指針賦值。通過函數指針調用其指向函數的寫法為:函數指針名(實參表);
快速排序庫函數:void qsort(void* base, int nelem, unsigned int width, int(*pfCompare)(const void*, const void*));
base:起始地址,nelem:元素個數,width:每個元素的大小(字節為單位),pfCompare:比較函數的地址。
?
作業
1.指針練習:輸出Hello
Description:下面程序片段的輸出結果是 Hello ,請填空。
1 #include <iostream> 2 using namespace std; 3 int main() { 4 char s[] = "Hello"; 5 char * p; 6 for( 7 // Your Code Here 8 ) 9 cout << * p ; 10 return 0; 11 }Input:(無)
Output:Hello
Sample Input:(無)
Sample Output:Hello
p=s; *p; p++2.指針練習:輸出Tesla
Description:下面程序輸出結果是 Tesla Tes 請填空。
1 #include <iostream> 2 using namespace std; 3 void Print(const char * p1, const char * p2) 4 { 5 for( 6 // Your Code Here 7 ) 8 cout << * p1; 9 } 10 int main() 11 { 12 const char * s = "Tesla123"; 13 Print(s,s+5); 14 cout << endl; 15 Print(s,s+3); 16 cout << endl; 17 18 return 0; 19 }Input:(無)
Output:
Tesla
Tes
Sample Input:(無)
Sample Output:
Tesla
Tes
; p1<p2; p1++3.指針練習:ForEach
Description:程序填空,使得輸出結果為:1,4,9,16,25, \nh,e,l,l,o,!,
1 #include <iostream> 2 using namespace std; 3 4 void ForEach(void * a, int width, int num, 5 // Your Code Here 6 ) 7 8 { 9 for(int i = 0;i < num; ++i) 10 f((char*)a+width*i); 11 } 12 13 void PrintSquare(void * p) 14 { 15 int * q = (int*)p; 16 int n = *q; 17 cout << n * n << ","; 18 } 19 void PrintChar(void * p) { 20 char * q = (char*)p; 21 cout << *q << ","; 22 } 23 int main() 24 { 25 int a[5] = {1,2,3,4,5}; 26 char s[] = "hello!"; 27 ForEach(a,sizeof(int),5,PrintSquare); 28 cout << endl; 29 ForEach(s,sizeof(char),6,PrintChar); 30 return 0; 31 }Input:(無)
Output:
1,4,9,16,25,
h,e,l,l,o,!,
Sample Input:(無)
Sample Output:
1,4,9,16,25,
h,e,l,l,o,!,
void (*f)(void*)4.指針練習:Memcpy之一
Description:程序填空,使得程序按要求輸出。
1 #include <iostream> 2 using namespace std; 3 void Memcpy(char * src,char * dest,int n) 4 { 5 // Your Code Here 6 } 7 int Strlen(char * s) 8 { 9 int i; 10 for( i = 0; s[i]; ++i); 11 return i; 12 } 13 int main() 14 { 15 int a; 16 char s1[30]; 17 char s2[30]; 18 int t; 19 cin >> t; 20 for(int i = 0;i < t; ++i) { 21 cin >> a; 22 int b = 99999999; 23 Memcpy((char*)&a,(char *) &b,sizeof(int)); 24 cout << b << endl; 25 } 26 for(int i = 0;i < t; ++i) { 27 cin >> s1; 28 Memcpy(s1,s2,Strlen(s1)+1); 29 cout << s2 << endl; 30 } 31 return 0; 32 }Input:第一行是整數t,接下來是t個整數,再接下來是t個不帶空格的字符串,長度不超過20。
Output:按原樣輸出t個整數和t個字符串。
Sample Input:
2
12
24
abcd
ef
Sample Output:
12
24
abcd
ef
for(int i=0; i<n; i++)dest[i] = src[i];5.指針練習:double
Description:程序填空,使其輸出結果是: 1,2,3,4, 10,12,14,16, 18,20,11,12,
1 #include <iostream> 2 using namespace std; 3 4 void Double(int * p, int n) 5 { 6 for(int i = 0;i < n; ++i) 7 p[i] *= 2; 8 } 9 10 11 int main() 12 { 13 int a[3][4] = { { 1,2,3,4},{5,6,7,8}, 14 { 9,10,11,12 } }; 15 16 Double( 17 // Your Code Here 18 ); 19 for(int i = 0;i < 3; ++i) { 20 for(int j = 0; j < 4; ++j) 21 cout << a[i][j] << ","; 22 cout << endl; 23 } 24 25 return 0; 26 }Input:(無)
Output:
1,2,3,4,
10,12,14,16,
18,20,11,12,
Sample Input:(無)
Sample Output:
1,2,3,4,
10,12,14,16,
18,20,11,12,
a[1], 66.指針練習:Memcpy之二
Description:程序填空,使得程序按要求輸出。
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 void Memcpy( void * src, void * dest, int size) 5 { 6 // Your Code Here 7 } 8 9 void Print(int * p,int size) 10 { 11 for(int i = 0;i < size; ++i) 12 cout << p[i] << ","; 13 cout << endl; 14 } 15 16 int main() 17 { 18 int a[10]; 19 int n; 20 cin >> n; 21 for(int i = 0;i < n; ++i) 22 cin >> a[i]; 23 int b[10] = {0}; 24 Memcpy(a,b,sizeof(a)); 25 Print(b,n); 26 27 int c[10] = {1,2,3,4,5,6,7,8,9,10}; 28 Memcpy(c,c+5,5*sizeof(int)); //將c的前一半拷貝到后一半 29 Print(c,10); 30 31 char s[10] = "123456789"; 32 Memcpy(s+2,s+4,5); //將s[2]開始的5個字符拷貝到s[4]開始的地方 33 cout << s << endl; 34 35 strcpy(s, "123456789"); 36 Memcpy(s+5,s+1,4); //將s[5]開始的4個字符拷貝到s[1]開始的地方 37 cout << s << endl; 38 39 40 return 0; 41 }Input:第一行是整數n (1<=n<=10),第二行是 n個整數。
Output:
先原序輸出輸入數據中的n個整數,然后再輸出:
1,2,3,4,5,1,2,3,4,5,
123434567
167896789
Sample Input:
10
15 25 35 45 55 65 75 85 95 105
Sample Output:
15,25,35,45,55,65,75,85,95,105,
1,2,3,4,5,1,2,3,4,5,
123434567
167896789
1 char* csrc = (char*)src; 2 char* cdest = (char*)dest; 3 if(src == dest) 4 return; 5 if(cdest>csrc && cdest<csrc+size) { 6 for(int i=size-1; i>=0; i--) 7 cdest[i] = csrc[i]; 8 } 9 else { 10 for(int i=0; i<size; i++) 11 cdest[i] = csrc[i]; 12 }7.指針練習:MyMax
Description:編寫一個 MyMax函數,可以用來求任何數組中的最大值 使得程序按要求輸出
1 #include <iostream> 2 using namespace std; 3 // Your Code Here 4 int Compare1(void * n1,void * n2) 5 { 6 int * p1 = (int * )n1; 7 int * p2 = (int * )n2; 8 return ((*p1)%10) - ((*p2)%10); 9 } 10 int Compare2(void * n1,void * n2) 11 { 12 int * p1 = (int * )n1; 13 int * p2 = (int * )n2; 14 return *p1 - *p2; 15 } 16 #define eps 1e-6 17 int Compare3(void * n1,void * n2) 18 { 19 float * p1 = (float * )n1; 20 float * p2 = (float * )n2; 21 if( * p1 - * p2 > eps) 22 return 1; 23 else if(* p2 - * p1 > eps) 24 return -1; 25 else 26 return 0; 27 } 28 29 int main() 30 { 31 int t; 32 int a[10]; 33 float d[10]; 34 cin >> t; 35 while(t--) { 36 int n; 37 cin >> n; 38 for(int i = 0;i < n; ++i) 39 cin >> a[i]; 40 for(int i = 0;i < n; ++i) 41 cin >> d[i]; 42 int * p = (int *) MyMax(a,sizeof(int),n,Compare1); 43 cout << * p << endl; 44 p = (int *) MyMax(a,sizeof(int),n,Compare2); 45 cout << * p << endl; 46 float * pd = (float * )MyMax(d,sizeof(float),n,Compare3); 47 cout << * pd << endl; 48 } 49 return 0; 50 }Input:第一行是測試數據組數t,對每組數據:第一行是整數n (1<=n<=10),第2行是n個整數,第3行是n個浮點數。
Output:對每組數據:先輸出n個整數中個位數最大的數(答案保證唯一),再輸出n個整數中最大的數,再輸出n個浮點數中最大的數。
Sample Input:
2
5
31 20 100 7 8
30.1 100.2 2.5 9.8 48.4
2
1 2
0.1 0.2
Sample Output:
8
100
100.2
2
2
0.2
1 void* MyMax(void* a, int width , int num, int (*compare)(void* p1,void* p2)) 2 { 3 void* result = a; 4 for(int i=1; i<num; i++) { 5 if(compare(result, ((char*)a)+i*width)<0) 6 result = ((char*)a)+i*width; 7 } 8 return result; 9 }8.指針練習:指向指針的指針
Description:程序填空使得輸出指定結果。
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int x,y,z; 6 x = 10; 7 y = 20; 8 z = 30; 9 10 int * a[3] = { &x, &y,&z}; 11 for( 12 // Your Code Here 13 p < a + 3; ++p) 14 cout<< * (*p) << endl; 15 return 0; 16 17 }Input:(無)
Output:
10
20
30
Sample Input:(無)
Sample Output:
10
20
30
int ** p = a;9.指針練習:SwapMemory
Description:填寫內存交換函數 SwapMemory,使得程序輸出指定結果。
1 #include <iostream> 2 using namespace std; 3 void SwapMemory(void * m1,void * m2, int size) 4 { 5 // Your Code Here 6 } 7 8 void PrintIntArray(int * a,int n) 9 { 10 for(int i = 0;i < n; ++i) 11 cout << a[i] << ","; 12 cout << endl; 13 } 14 15 int main() 16 { 17 int a[5] = {1,2,3,4,5}; 18 int b[5] = {10,20,30,40,50}; 19 SwapMemory(a,b,5 * sizeof(int)); 20 PrintIntArray(a,5); 21 PrintIntArray(b,5); 22 char s1[] = "12345"; 23 char s2[] = "abcde"; 24 SwapMemory(s1,s2,5); 25 cout << s1 << endl; 26 cout << s2 << endl; 27 return 0; 28 }Input:(無)
Output:
10,20,30,40,50,
1,2,3,4,5,
abcde
12345
Sample Input:(無)
Sample Output:
10,20,30,40,50,
1,2,3,4,5,
abcde
12345
1 char* p1 = (char*)m1; 2 char* p2 = (char*)m2; 3 for(int i=0; i<size; i++) { 4 char tmp = p1[i]; 5 p1[i] = p2[i]; 6 p2[i] = tmp; 7 }?
轉載于:https://www.cnblogs.com/VincentValentine/p/5676318.html
總結
- 上一篇: 自定义View-实现简易车速器(真的够简
- 下一篇: 另类玩法:通过 DNS 进行文件传输