內建類型的指標、引用,sizeof大小、虛函數大小
1 char yinyong = 10;
2 char &z = yinyong;
3 cout << sizeof(z) << endl;
4
5 int intee = 10;
6 int &intez = intee;
7 cout << sizeof(intez) << endl;
8
9 char *zz = &yinyong;
10 cout << sizeof(zz) << endl;
輸出結果為1 4 4
內建類型的指標的sizeof值是:4
代碼
1 #include <iostream>
2 using namespace std;
3
4 class Base{
5 public:
6 Base(){
7 cout << "construction" << endl;
8 }
9 virtual void myfun(){};
10 private:
11 int ival;
12 };
13
14 void main()
15 {
16 Base mybase;
17 cout << "sizeof mybase is : " << sizeof mybase << "\nEND" << endl;
18 }
Output:
虛函數、整型ival各需要4 byte,總計8byte。
如果注釋掉9、10、11行,輸出大小為1。也就是說一個類內沒有任何成員變數、成員函數、虛函數,它的sizeof大小為 1 byte。
string對象中的字元處理
我們經常要對string對象中的單個字元進行處理,例如通常需要知道某個特殊字元是否為空白字元、字母或者數字。這些函數定義在cctype標頭檔中。
isdigit(c) 是否是數字 islower(c) 是小寫字母 isupper(c) 是大寫字母 isspace(c) 是空白字元
ispunct(c) 是標點符號 tolower(c) 轉換成小寫字母 toupper(c) 轉換成大寫字母 isalpha(c) 是字母?
解引用操作符* 和 自增操作符++ 的優先順序
*iter++
++的優先順序高於*。
string實際上也是C-style 風格字串
string mystring = "meng";
cout << mystring.size() << endl;
if(mystring[4] == NULL)
cout << "true" << endl;
啟動並執行結果是,4 true
這說明string對象的末尾也有個NULL。
mystring.length() 結果也是4。
vector, list, deque 容器
vector, deque都支援容器的隨即訪問,vector插入刪除非末端元素耗費代價高。deque插入刪除非兩端位置元素代價高。
list不支援隨機訪問元素,但可以在任意位置插入刪除元素而消耗很低的代價。
1 list<int> ilist;
2 vector<int> ivec;
3 deque<int> ideq;
4 ilist[3]; //ERROR
5 ideq[3]; //OK
6 ivec[3]; //OK
關於string
1 string str = "Stately plump Buck";
2 char *cp = "Stately plump Buck";
3 string s1(str, 7), s2;
4 s2.assign(cp, 7);
5 cout << s1 << endl;
6 cout << s2 << endl;
列印結果為
空格plump Buck
Stately
空類的sizeof大小
空類指的是裡面只有非virtual成員函數的類,並且不繼承任何類,無成員變數。其sizeof大小為1。