C + + Programming attention issues __linux

Source: Internet
Author: User

1, when the string type is initialized with two iterators, such as String str (Str1.begin (), Str1.begin () +5), in fact, the above is an interval open after the opening, that is to say, Str1,begin () + 5 The characters pointed to are not copied.

2, void * memset (void * ptr, int value, size_t num); initializes the first Num area pointed to by PTR with value, an initialization method.

3, to determine whether two floating-point types of variables are equal do not use a==b form, but should determine the difference between the absolute value of two numbers is less than a threshold.

It is not correct to compare floating-point data types directly using the operator = =, <=, >=,!=, and so on in your code. The correct approach should be to compare the difference or the decimal.

4, array pointer and array of pointers silly to know: int *p[], first you need to understand [] the priority is higher than *,p first and [] combined, indicating that it is an array, and * union, indicating that the variables in the array are pointers; int (*p) [],p First and *, so p is a pointer, and [] combines , which indicates that the pointer points to an array.

5, the string, the first use string, encountered a variable array, the use of vector first, you can use STL don't write yourself.

6, size_t type use special attention, it cannot be negative, so do not commit size_t T;while (t>=0) {--t}; Such a mistake, because t can never be negative.

7, to determine whether an integer is odd, with x%2!=0, do not use x%2==1, because x may also be negative.

8, after using the new must use Delete, they and (), {},[] like, are in pairs appear.

9, if (t), while (t), and so on, the following omitted are "!=0/null"; True 1,false is 0;a==b this form, equal (true) returns 1, unequal (for false) returns 0;

10, STL Standard Template Library is a part of C + + standard library, C + + standard library and HP,SGI, such as the implementation of STL version, the most of the STL Standard Template Library is implemented in the SGI; however, in the use of so much, the STL standard library header file is not an extension of the name, Directly include the relevant header file; STL has a lot of practical algorithms, familiar with its use can save a lot of time, and STL implementation of the version of the performance will not be worse than their own write to where.

11, Private: Only by 1. The function in this class, 2. Its friend function access. cannot be accessed by any other object, nor can objects of that class be accessed. Protected: can be accessed by 1. Functions in the class, 2. Subclass functions, and 3. Its friend function access. But cannot be accessed by objects of this class.  Public: can be accessed by 1 functions in the class, functions in 2. Subclasses, 3. Its friend function, or by 4. The object of the class. Note: The friend function includes 3 kinds: a normal, non-member function that is set as a friend, a member function of another class that is set as a friend, and is set to all member functions in a friend class.

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.