Signed number, unsigned tree mixed calculation problem.

Source: Internet
Author: User

Today, online brush questions, one of the problems always results and expectations are not the same, in time and again check the program logic, confirm the correct, or can not pass, forced to use vs Start debugging!

Here is my program code:

1 //maxDepth.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <vector>6#include <iostream>7#include <limits.h>8 9 using namespacestd;Ten  One classTreeNode { A  Public: -      intVal; -TreeNode *left, *Right ; theTreeNode (intval) { -           This->val =Val; -           This->left = This->right =NULL; -      } +  };  -  + classSolution { A  Public: at     /** - * @param root:the root of binary tree. - * @return: An integer -      */ -     voidPreorder (TreeNode *root, Std::vector<treenode*> &path) -     { in         if(!root) -             return; to Path.push_back (root); +         if(!root->left &&!root->Right ) -             if(Path.size () >max) theMax =path.size (); *         if(Root->left) Preorder (root->Left , path); $         if(Root->right) Preorder (root->Right , path);Panax Notoginseng Path.pop_back ();  -     } the     intMaxDepth (TreeNode *root) { +         //Write your code here A         if(!root) the             return 0; +Std::vector<treenode*>path; - preorder (root, path); $         returnMax; $     } - solution (): Max (int_min) {;} - Private: the     intMax; - };Wuyi  the voidTest () - { Wu     intc =-1; -     if(C > (unsigned)1) Std::cout <<"C >-1"<<Std::endl; Aboutc =int_min; $     //std::cout << C; -     //std::cout << -      -Std::cout <<-7+ (unsigned)5<<Std::endl; AStd::cout <<-7+ (unsigned)Ten<<Std::endl; +  the     //bool B = c > 1; -     //std::cout << B; $  the } the  the int_tmain (intARGC, _tchar*argv[]) the { -TreeNode *root =NewTreeNode (0); in solution so; theStd::cout << so.maxdepth (Root) <<Std::endl; the  About Test (); the  the  the      +     return 0; -}

When you debug, you find that if (Path.size () > Max) is not executed at line 33. So it's clear that there is a mixed operation problem with signed and unsigned numbers!

1. int Max defines a signed number and is initialized to the smallest negative number int_min, and the size () function returns an unsigned count, in which the signed number is converted to an unsigned number. The first digit of a negative number is 1, and is converted to an unsigned count greater than all positive numbers, so there's no way to do this!

2. After the conversion process, the overflow may occur, C is a simple way to truncate! As I am here, the test () function compiles information.

3. In fact, when I compile, VS has issued a warning to 33 rows "warning C4018:" > ": Signed/unsigned mismatch", but I ignored this approach. Therefore, never ignore any warnings from the compiler.

4. In fact, in chapter II of Csapp, there is an in-depth discussion of the signed and unsigned number. It is concluded that "the disadvantage of unsigned number is much greater than the effect of it, and the use of unsigned number is less." However, in the C + + standard library, a lot about size (), the capacity () function returns unsigned numbers, so you must handle the problem of the mixed operation of signed unsigned numbers with care in such a program!

Signed number, unsigned tree mixed calculation problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.