Today, my senior friend talked about the C ++ questions during the recruitment of the company. After listening to these questions, I felt that C ++ was untested. I really need to check out objective C ++. Of course, just share your feelings ....
1. The first is to exchange two data items. We usually use the third variable, so we can know at most how to exchange the real number without the third variable. What is the addition ~~~ But, said the senior brother, which method cannot be used, myGod. Is there a third method ~~~ If I go to the interview, I will die. So what is the method, that is, it can be an exclusive or, efficient, and can process any data type. As follows:
View plaincopy to clipboardprint?
-
- >>># Swap two integer number
-
- >>> A =10
-
- >>> B =23
-
- >>>Print "The A = {0}, B = {1} before swapping". Format (A, B)
- The A =10, B =23Before swapping
-
- >>> A ^ = B
-
- >>> B ^ =
-
- >>> A ^ = B
-
- >>>Print "The A = {0}, B = {1} After swapping". Format (A, B)
-
- The A =23, B =10After swapping
2. Global variables are placed in the heap, while local variables are placed in the stack, such as new and static variables that open up different or declarative variables are all placed in the heap. Therefore, a local pointer cannot be returned in a function, as shown in figure
View plaincopy to clipboardprint?
- VoidFoo (Char* Pchar)
- {
- Pchar =New Char[10];
- CharTempstr [] ="Hexiong";
- Strcpy (pchar, tempstr );
- Pchar [strlen (tempstr)] ='\ 0';
- }
In this case, an error is reported. Of course, there are two solutions. The first one is to use reference ....
View plaincopy to clipboardprint?
- VoidFoo (Char* & Pchar)
- {
- Pchar =New Char[10];
- CharTempstr [] ="Hexiong";
- Strcpy (pchar, tempstr );
- Pchar [strlen (tempstr)] ='\ 0';
- }
Second, use **
View plaincopy to clipboardprint?
- VoidFoo (Char** Pchar)
- {
- * Pchar =New Char[10];
- CharTempstr [] ="Hexiong";
- Strcpy (* pchar, tempstr );
- (* Pchar) [strlen (tempstr)] ='\ 0';
- }
Of course, I prefer the first method .....
----------------- Create high-qualityArticleFor more information, thank you ---------------
To create high-quality articles, pleaseRecommendationCome on .... Thank you. Please pay attention to my subsequent articles, which will be even better.
Sina Weibo: http://weibo.com/baiyang26
The wine ghost official blog: http://www.ibaiyang.org [recommended to use Google Reader subscription]
Pour wine into the official Douban: http://www.douban.com/people/baiyang26/
If you want to repost this blog,Please specify the source
If you have any comments or suggestions on this article,Leave a message