File tree Expand file tree Collapse file tree 8 files changed +370
-0
lines changed
Expand file tree Collapse file tree 8 files changed +370
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: 2.2.1.cpp
3+ * @author: Tanswer
4+ * @date: 2018年02月23日 13:30:25
5+ * @description:
6+ */
7+
8+ #include < iostream>
9+ using namespace std ;
10+
11+ class Test {
12+
13+ Test ();
14+ virtual ~Test ();
15+
16+ };
17+
18+ int main ()
19+ {
20+ cout << sizeof (Test) << endl;
21+
22+
23+ return 0 ;
24+ }
Original file line number Diff line number Diff line change 1+
2+ /*
3+ * @filename: 2.2.1_2.cpp
4+ * @author: Tanswer
5+ * @date: 2018年02月23日 13:40:56
6+ * @description:
7+ */
8+
9+ #include < iostream>
10+ using namespace std ;
11+
12+ class A
13+ {
14+ public:
15+ A (int n) { value = n; }
16+ A (A other) { value = other.value ; }
17+ // error 拷贝构造函数的参数必须是引用
18+
19+ void Print () { cout << value << endl; }
20+ private:
21+ int value;
22+ };
23+
24+
25+ int main ()
26+ {
27+ A a = 10 ;
28+ A b = a;
29+ b.Print ();
30+
31+ return 0 ;
32+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: CMyString.cpp
3+ * @author: Tanswer
4+ * @date: 2018年02月23日 13:47:46
5+ * @description:
6+ */
7+
8+ #include < iostream>
9+ #include < cstring>
10+ using namespace std ;
11+
12+ class CMyString
13+ {
14+ public:
15+ CMyString (char * pData = NULL );
16+ CMyString (const CMyString& str);
17+ ~CMyString (void );
18+
19+ CMyString& operator =(const CMyString& str);
20+ private:
21+ char * m_pData;
22+ };
23+
24+ CMyString& CMyString::operator =(const CMyString& str)
25+ {
26+ // 检测自我赋值
27+ if (&str == this )
28+ return *this ;
29+
30+ delete [] m_pData; // 释放实例自身已有内存
31+ m_pData = NULL ;
32+ m_pData = new char [ strlen (str.m_pData ) + 1 ]; // 申请空间
33+ strcpy (m_pData,str.m_pData ); // 复制
34+
35+ return *this ; // 返回引用,允许连续赋值
36+ }
37+
38+ /*
39+ * 考虑异常安全性的解法
40+ * 如果delete 后 内存不足导致 new char 抛出异常
41+ *
42+ CMyString& CMyString::operator=(const CMyString& str)
43+ {
44+ if(this != &str)
45+ {
46+ CMyString strTemp(str);
47+ char *pTemp = strTemp.m_pData;
48+ strTemp.m_pData = m_pData;
49+ m_pData = pTemp;
50+ }
51+
52+ return *this;
53+ }
54+ */
55+
56+ CMyString::CMyString (char * pData) // 实现的时候不用写默认值
57+ {
58+ if (pData == NULL )
59+ {
60+ m_pData = new char [1 ];
61+ *m_pData = ' \0 ' ;
62+ } else {
63+ m_pData = new char [ strlen (pData)+1 ];
64+ strcpy (m_pData,pData); // 复制
65+ }
66+ }
67+
68+ // 拷贝构造,参数必须为引用
69+ CMyString::CMyString (const CMyString& str)
70+ {
71+ if (str.m_pData == NULL )
72+ {
73+ m_pData = new char [1 ];
74+ *m_pData = ' \0 ' ;
75+ } else {
76+ m_pData = new char [ strlen (str.m_pData )+1 ];
77+ strcpy (m_pData, str.m_pData );
78+ }
79+ }
80+
81+ CMyString::~CMyString ()
82+ {
83+ if (m_pData != NULL )
84+ {
85+ delete [] m_pData;
86+ }
87+ }
88+
Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: Singleton_1.cpp
3+ * @author: Tanswer
4+ * @date: 2018年02月23日 15:59:27
5+ * @description: 只适用于单线程环境
6+ */
7+
8+ #include < iostream>
9+ using namespace std ;
10+
11+ class Singleton
12+ {
13+ public:
14+ static Singleton* GetInstance ()
15+ {
16+ if (m_pInstance == NULL )
17+ {
18+ m_pInstance = new Singleton ();
19+ }
20+ return m_pInstance;
21+ }
22+
23+ private:
24+ Singleton ();
25+ static Singleton* m_pInstance;
26+ };
Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: 二维数组的查找.cpp
3+ * @author: Tanswer
4+ * @date: 2018年02月23日 17:25:11
5+ * @description:
6+ */
7+
8+ #include < iostream>
9+ using namespace std ;
10+
11+
12+ /*
13+ * 从右上角的元素开始判断
14+ * 如果待查找元素等于该元素,就返回 true
15+ * 如果待查找元素小于该元素,排除该元素所在行
16+ * 如果待查找元素大于该元素,派出该元素所在列
17+ * 直到找到或者范围为空
18+ *
19+ * 二维数组在内存中占据连续空间,从上到小 从左到右存储
20+ */
21+ bool Find (int * matrix, int rows, int cols, int num)
22+ {
23+ bool res = false ;
24+
25+ if (matrix != NULL && rows > 0 && cols > 0 ){
26+ int row = 0 ;
27+ int col = cols - 1 ;
28+ while (row < rows && col > 0 ){
29+ if (matrix[row*cols + col] == num){
30+ res = true ;
31+ break ;
32+ }
33+ else if (matrix[row*cols + col] > num){
34+ col--;
35+ }
36+ else
37+ row++;
38+ }
39+ }
40+ return res;
41+ }
42+
43+
Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: 从尾到头输出链表.cpp
3+ * @author: Tanswer
4+ * @description: 用栈实现
5+ */
6+
7+ #include < iostream>
8+ #include < stack>
9+ using namespace std ;
10+
11+ typedef struct ListNode
12+ {
13+ int m_data;
14+ ListNode* next;
15+ }ListNode;
16+
17+ ListNode* CreateList ()
18+ {
19+ ListNode* head;
20+ ListNode* node;
21+ head = (ListNode*)malloc (sizeof (ListNode));
22+ head -> m_data = 0 ;
23+ head -> next = NULL ;
24+ node = head;
25+
26+ while (1 )
27+ {
28+ int data;
29+ cin >> data;
30+ if (data != 0 )
31+ {
32+ ListNode *pNew;
33+ pNew = (ListNode*)malloc (sizeof (ListNode));
34+ pNew -> m_data = data;
35+ pNew -> next = NULL ;
36+
37+ node -> next = pNew;
38+ node = node -> next;
39+ }
40+ else
41+ break ;
42+ }
43+ return head -> next; // 无头
44+ }
45+
46+ // 栈实现
47+ void PrintListReversingly_Iteratively (ListNode* pHead)
48+ {
49+ stack<ListNode*> nodes;
50+ ListNode *c_node = pHead;
51+ while (c_node != NULL )
52+ {
53+ nodes.push (c_node);
54+ c_node = c_node -> next;
55+ }
56+
57+ while (!nodes.empty ())
58+ {
59+ c_node = nodes.top ();
60+ cout << c_node -> m_data << ' \t ' ;
61+ nodes.pop ();
62+ }
63+ cout << endl;
64+ }
65+
66+ // 递归实现 结点很多可能会导致函数调用栈溢出
67+ void PrintListReversingly_Recursively (ListNode* pHead)
68+ {
69+ if (pHead != NULL )
70+ {
71+ if (pHead -> next != NULL )
72+ PrintListReversingly_Recursively (pHead -> next);
73+
74+ cout << pHead->m_data << ' \t ' ;
75+ }
76+ }
77+
78+ int main ()
79+ {
80+ ListNode* head;
81+ head = CreateList ();
82+
83+ PrintListReversingly_Iteratively (head);
84+ PrintListReversingly_Recursively (head);
85+ return 0 ;
86+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * @filename: 替换空格.cpp
3+ * @author: Tanswer
4+ * @description:
5+ */
6+
7+ #include < iostream>
8+ using namespace std ;
9+
10+ /*
11+ * @string:字符数组
12+ * @length:总容量
13+ */
14+ bool ReplaceBlank (char string[], int length)
15+ {
16+ if (string == NULL || length <=0 )
17+ return false ;
18+
19+ int oldLength = 0 ;
20+ int numOfBlank = 0 ;
21+ int i = 0 ;
22+ while (string[i] != ' \0 ' ){
23+ oldLength++;
24+ if (string[i] == ' ' )
25+ numOfBlank++;
26+
27+ i++;
28+ }
29+
30+ int newLength = oldLength + numOfBlank * 2 ;
31+ if (newLength > length)
32+ return false ;
33+
34+ cout << newLength << " old " << oldLength << endl;
35+ int oldIndex = oldLength;
36+ int newIndex = newLength;
37+ while (oldIndex != newIndex){
38+ if (string[oldIndex] == ' ' )
39+ {
40+ cout << " lalala" << endl;
41+ string[newIndex--] = ' 0' ;
42+ string[newIndex--] = ' 2' ;
43+ string[newIndex--] = ' %' ;
44+ }
45+ else
46+ {
47+ string[newIndex--] = string[oldIndex];
48+ }
49+
50+ oldIndex--;
51+ }
52+ return true ;
53+ }
54+
55+ int main ()
56+ {
57+ char str[50 ] = " we are happy." ;
58+ char *str1 = NULL ;
59+ char str2[20 ] = " " ;
60+
61+ ReplaceBlank (str, 50 );
62+ ReplaceBlank (str1, 50 );
63+
64+ cout << ReplaceBlank (str2, 20 ) << endl;;
65+
66+ cout << str << endl;
67+ cout << str1 << endl;
68+ cout << str2 << endl;
69+
70+ return 0 ;
71+ }
You can’t perform that action at this time.
0 commit comments