7-19晚牛客网刷题未知点、错题 集合
1.
初始化為NULL(0)的類指針可以安全的調用不涉及類成員變量的類成員函數而不出錯,但是如果類成員函數中調用了類成員變量則會出錯
?
2.
悄咪咪加一個注意點:
注意enum在c語言中是關鍵字 ,c語言對大小寫比較敏感,所以ENUM是一個標識符,但不是關鍵字。
?
3.
#include<iostream>
using?namespace?std;
class?TestClass{
????char?x;
public:
????TestClass() { cout <<?'A'; }
????TestClass(char?c) { cout << c; }
????~TestClass() { cout <<?'B'; }
};
int?main() {
????TestClass p1, *p2;
????p2 =?new?TestClass('X');
????delete?p2;
????return?0;
}
解析:
TestClass p1, *p2; //只為p1調用默認構造——A?
p2 =?new TestClass('X'); //調用構造函數,由p2指向——X?
delete p2; //釋放內存空間,p2所指實例調用析構函數——B?
return 0; //程序結束,p1調用析構——B
?
?
4,5 兩個還是比較混亂的 正在研究中
4.
單選:
下列 C 代碼中,不屬于未定義行為的有:
A: int i=0;i=(i++);
B: char *p=”hello”;p[1]=’E’
C: char *p=”hello”;char ch=*p++
D: int i=0;printf(“%d%d\n”,i++,i--)
?
5.
下面代碼的輸出是什么?
class?A ?
{ ?
public: ?
????A() ?{ ?? ?} ?
????~A() { ? ?cout<<"~A"<<endl; ? } ?
}; ?
???
class?B:public?A ?
{ ?
????public: ?
??? ? ??B(A &a):_a(a) ?
??? ? ??{ ?
??? ? ? ? ???
??? ? ??} ?
??? ? ??~B() ?
??? ? ??{ ?
??? ? ? ? ??cout<<"~B"<<endl; ?
??? ? ??} ?
????private: ?
??? ? ??A _a; ?
????}; ?
??? ???
int?main(void) ?
?{ ?
??? ? ??A a; ? ? ??//很簡單,定義a的時候調用了一次構造函數 ?
??? ? ??B b(a);?
}
答案:
~B
~A
~A
~A
?
?
7.
假定CSomething是一個類,執行下面這些語句之后,內存里創建了____個CSomething對象。
CSomething a();
CSomething b(2);
CSomething c[3];
CSomething &ra = b;
CSomething d=b;
CSomething *pA = c;
CSomething *p =?new CSomething(4);
?
解析:6個
CSomething a();//?沒有創建對象,這里不是使用默認構造函數,而是定義了一個函數,在C++ Primer393頁中有說明。
CSomething b(2);//使用一個參數的構造函數,創建了一個對象。
CSomething c[3];//使用無參構造函數,創建了3個對象。
CSomething &ra=b;//ra引用b,沒有創建新對象。
CSomething d=b;//使用拷貝構造函數,創建了一個新的對象d。
CSomething *pA = c;//創建指針,指向對象c,沒有構造新對象。
CSomething *p = new CSomething(4);//新建一個對象。
?
8.
假設所有變量均為整型,則表達式(a=2,b=5,b++,a+b)的值是____。
8
解析:
逗號表達式的求值順序是從左向右依次計算用逗號分隔的各表達式的值,最
后一個表達式的值就是整個逗號表達式的值。所以表達式最后的值是a+b,但經過前面的
計算,此時a的值為2,b的值是6,所以最后的結果是8,應選擇B。
?
9.
下面這個代碼輸出的是()
#include <iostream> ? ? ??
#include <vector>
using?namespace?std;
int?main(void)
{
????vector<int>array;
????array.push_back(100);
????array.push_back(300);
????array.push_back(300);
????array.push_back(300);
????array.push_back(300);
????array.push_back(500);
????vector<int>::iterator itor;
????for(itor=array.begin();itor!=array.end();itor++)
????{
????????if(*itor==300)
????????{
????????????itor=array.erase(itor);
????????}
????}
????for(itor=array.begin();itor!=array.end();itor++)
????{
????????????cout<<*itor<<"";
????}
??return?0;
}
?
100 300 300 500
解析:
vector::erase():從指定容器刪除指定位置的元素或某段范圍內的元素?
vector::erase()方法有兩種重載形式?
如下:?
iterator erase(?? iterator _Where);?
iterator erase(?? iterator _First,?? iterator _Last);?
如果是刪除指定位置的元素時:?
返回值是一個迭代器,指向刪除元素下一個元素;?
如果是刪除某范圍內的元素時:返回值也表示一個迭代器,指向最后一個刪除元素的下一個元素;
?
在本題中,當?*itor==300成立時,刪除第一個值為300的元素,同時itor指向下一個元素(即是第二個值為300的元素),
? ? ? ? ? ? ? ? ? ? ? ? ? ??在for(;;itor++)執行itor,itor指向第三個值為300的元素,進入下一個循環
? ? ? ? ?進入循環滿足*itor==300,重復上面的過程,執行完循環,itor執行值為500的元素。
所有整個過程中,只刪除了2個值為300的元素。
?
?
?
?
?
總結
以上是生活随笔為你收集整理的7-19晚牛客网刷题未知点、错题 集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7-19下午刷题未知点集合
- 下一篇: 7-20上午刷题未知点集合