随笔分类 - C++
代码、学习记录
摘要:字符串转换成整形值,能够考虑的异常因素有很多。编写代码之前设计测试用例很有帮助。 1 enum STATUS{STRNULL=0, STREMPTY, STRINVALID, STROVERFLOW, STRVALID}; 2 enum SIGN{NEGATIVE=0, POSITIVE}; 3 enum STATUS status = STRVALID; 4 enum SIGN sign = POSITIVE; 5 int str2int(const char *str) 6 { 7 long long num = 0; 8 9 if (str == NU...
阅读全文
摘要:根据二叉树的先序遍历和中序遍历重建二叉树。 1 #include 2 3 #include 4 5 using namespace std; 6 7 template 8 struct TreeNode 9 { 10 T m_nData; 11 12 TreeNode* m_pLeft; 13 14 TreeNode* m_pRight; 15 16 }; 17 18 19 template 20 TreeNode* ConstructTree(const T* pPreTraval, const T* pInTraval, int...
阅读全文
摘要:异或运算是位运算的一种。我们知道位运算速度很快,同时因其运算的特点,给我们带来不同的解题思路来处理问题。异或运算,可以用来实现两个数的交换,而不必担心越界问题;异或运算可以找出数组中只出现一次的值;异或运算可以找出从一个数组中任意删除的一个值。 1 #include 2 3 using namespace std; 4 5 void main() 6 { 7 //交换a与b 8 int a = 10; 9 int b = 5;10 a = a ^ b;11 b = a ^ b;12 a = a ^ b;13 cout<<a<<","...
阅读全文
摘要:寻找单项链表中间那个元素,如果有两个则取前面一个。 1 #include 2 #include 3 4 using namespace std; 5 6 template 7 struct Node 8 { 9 T m_nData;10 Node *m_pNext;11 };12 13 template14 Node* createList(const T myArray[], const int &n)15 //创建链表16 {17 assert(myArray && n >0);18 Node* pHead = new Node;19 No...
阅读全文
摘要:1 #include 2 #include 3 4 using namespace std; 5 6 template 7 void printArray(T array[],int arrayLen) 8 { 9 if (arrayLen > 1) 10 { 11 cout 22 void randomPerm(T array[],int arrayLen) 23 { 24 if (arrayLen > 1) 25 { 26 T ex; 27 int rand1,rand2; 28 ...
阅读全文
摘要:条款32:确定public继承is-a的意义符合你的设计意图ClassPerson;ClassStudent:publicPerson{};Voidstudy(constStudent&s);Voideat(constPerson&p);Personp;Students;eat(p);eat(s);Study(p);Study(s);Public继承代表的是is-a的关系。对于eat()函数,编译器可以将Student对象转型为Person对象,而对于study()函数而言,Person对象不一定是Student对象,因此反过来就行不通。如果想要程序编译通过,可能要进行强制类型
阅读全文

浙公网安备 33010602011771号