微眾銀行c++選擇題後記
阿新 • • 發佈:2017-09-29
類對象 stat 輸出 nbsp == span 獲得 const per
一個類的成員可以有:另一個類的對象,類的自身指針,自身類對象的引用(私有的如何初始化呢,所以不行,換成靜態的可以),自身類對象(構造時如何初始化呢?)
1 class A{ 2 public: 3 A(){} 4 A(A* a); 5 static A &reA; 6 private: 7 A *ptrA; 8 9 10 }; 11 A& A::reA = A();
考查*++p的運算: ++*p,先獲得p指向的變量,再對這個變量自增,所以c變成了d
1 char arr[] = "abcdefghij",*p=arr; 2 p += 2; 3 cout << *p++ << endl;//輸出c,形式*(p++)也是輸出c 4 cout << p << endl;//輸出defghij
char arr[] = "abcdefghij", *p = arr;
p += 2;
cout << (++*p) << endl; //輸出d ,
cout << p << endl; //輸出ddefghij
cout << arr << endl;
char arr[] = "abcdefghij",*p=arr; p += 1; cout << *++p << endl;//輸出c cout << p << endl;//指向c
條件運算符裏的求值順序:運算符?:有三個操作數:在a?b:c中,操作數a首先被求值,根據a的值再求操作數b或c的值。
1 int i = 5; 2 int j = 0; 3 int k = 0; 4 k = (++i) > (j) ? (++i) : (j); 5 cout << k << endl; //輸出7
free空指針,指針作為參數,也是傳遞的指針的值
會內存泄漏,程序不會崩潰
voidfoo(char *p) { p = (char*)malloc(100); strcpy(p, "hello"); } int main() { char *str = NULL; foo(str); if (!str) cout << "str is NULL" << endl; //結果為真 printf("%s", str); free(str); }
比較字符串用strcmp,函數依次比較字符的大小,依字典序,==比較的是兩個字符指針是否指向同一地址;string可以和const char*比較
struct stu{ char name[10]; }; int main() { stu person[3] = { { "AA" }, { "AA" }, { "BB" } }; if (strcmp(person[1].name, "AA") == 0) cout << "strcmp is valid" << endl;//比較的是字符串的內容 if (person[1].name == "AA") cout << " 比較的是兩個指針是否指向同一地址" << endl; string s1 = "abc"; string s2 = "abc"; cout << (s1 == "abc") << endl;// string類是可以用==和const char*比較的 char str1[] = "abc"; char str2[] = "abc"; const char str3[] = "abc"; const char str4[] = "abc"; const char* str5 = "abc"; const char* str6 = "abc"; cout << boolalpha << (str1 == str2) << endl; // 輸出什麽? cout << boolalpha << (str3 == str4) << endl; // 輸出什麽? cout << boolalpha << (str5 == str6) << endl; // 輸出什麽? }
返回字符數組指針和字符指針: 數組名不能自增,作為地址,是常量,不是左值
1 char *foo() 2 { 3 char *p = "hhd"; //返回p輸出正常 4 char q[] = "jjj";//返回q輸出未知 5 return q; 6 } 7 int main() 8 { 9 char *p = foo(); 10 cout << p << endl;
微眾銀行c++選擇題後記