Middle Body Chun Color C + + face Test __c++

Source: Internet
Author: User
Tags getmessage message queue rand strlen

The following is my memory thought of a few topics, there is a need for students to take it, I also counted to do a little charity.


Middle Body Chun Cai C + + pen Test
2013-11-18


1. The meaning of the pointer is: B
A. Name B. Address C. name D. symbol


2. Give the following program output:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <locale>
using namespace Std;

int main (int argc,char** argv) {
Char str[] = "Tao";
Char str1[] = "ztjcy in the body Chun Cai";
wchar_t str2[] = L "ztjcy in the body Chun Cai";
cout << strlen (str) << "," << sizeof (str) <<endl;
cout << strlen (str1) << "," << sizeof (STR1) <<endl; 17,18
Wcout << wcslen (str2) << "," << sizeof (STR2) <<endl; 9,40

return 0;
}
Note that Chinese characters account for 3 bytes, when UTF8 encoding, the wide character size is 2 bytes,




3. What is the difference between overloading (overload) and overriding (overwrite)?
Overloaded Overload: The function name is the same, and the argument list has different overloads that exist only within the class. But it cannot be judged by the type of return.
Rewrite override: Also called overlay. Subclasses redefine virtual functions that have the same name and parameters in the parent class. function features the same. But the specific implementation is different, mainly in the inheritance relationship appears.




4. Indicate the error of the following procedure?
Class a{
Public
A () {p = this;}
~a () {if (p) {
Delete p;
p = null;
}
}
Private
* p;
}




5. Thread A is running indefinitely, how does thread B allow thread A to exit safely?
There are many ways to exit threads, which are referred to as message passing, synchronization, sharing, etc. between threads.


Terminating a thread, you can send a message/signal to the thread, or a mutex, or change in memory variables, and so on, to inform the thread, when the thread received the message/signal, do some processing, such as releasing the resources within the thread, save the running results of the thread, and then exit the thread themselves, and return a uint value.




What is the difference between 6.TCP and UDP?
TCP---Transmission Control Protocol, which provides a connection-oriented, reliable byte throttling service. Before the client and server Exchange data, a TCP connection must be established between the two parties before the data can be transferred. TCP provides timeouts, discards duplicate data, verifies data, and controls traffic to ensure that data is transferred from one end to the other.
UDP---User Datagram Protocol is a simple transport layer protocol for datagram. UDP does not provide reliability, it simply sends the application to the IP layer, but it does not guarantee that it will reach its destination. Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as timeout, the transmission speed is fast




What is the difference between 7.PeekMessage () and GetMessage ()?
Same point:
Both the PeekMessage function and the GetMessage function are used to view application message queues and to distribute messages in queues when there is a message.
Different points:
The PeekMessage function returns immediately, regardless of whether there is a message in the application message queue, and the program continues to execute the following statement (no message executes other instructions, and when there is a message, the message is generally distributed and other instructions are executed). The GetMessage function returns only if there is a message in the message queue, and no messages in the queue wait until the next message appears. During this time, the application cannot execute any directives.
(from their different point of view, PeekMessage function is a bit like "beggar begging", you have to give points, no Rob. GetMessage function is a bit like "robber robbery", you have to give, no I will wait for you when you have to give again, this time I do nothing, I will wait for you. )




8. Enter an integer, output it as a comma-delimited string, such as 1234567890, and output as "1,234,567,890"?
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
This program is used to format numeric representations of numbers as "comma separated by thousands", such as: 23,343,434,436
String Str_number;
int str_length;
int swap_number;
cout<< "Please enter an integer >= 1000:" <<endl;
cin>>str_number;
Str_length = Str_number.length ();
Swap_number = str_length% 3;
if (swap_number!= 0)
{
Cout<<str_number.substr (0,swap_number);
for (int i=swap_number;i<=str_length-3;i=i+3)
{
cout<< "," <<str_number.substr (i,3);
}
}
Else
{
for (int i=0;i<=str_length-3;i=i+3)
{
Cout<<str_number.substr (i,3);
if (i!= str_length-3)
{
cout<< ",";
}
}
}
cout<<endl;
return 0;
}




9. Given an array of integers of size 100, it is required to randomly insert 1 ... The number in 100, the request cannot insert duplicate number, give the most efficient solution?
This problem is mainly based on the set data structure in STL and Rand () to achieve efficient solution. A feature of set data structure is that the key values are combined and sorted automatically.
Note here that the header file for rand () is the Stdlib.h file. Here is the reference code for the implementation




#include <stdlib.h>
#include <iostream>
#include <set>
using namespace Std;


void Create_rand (int len) {
if (len <= 0) {
cout << "Please input a invalid value:" << Endl;
Return
}


Set<int> Random_set;
do{
int val = rand ()%len;
if (val = 0)
val = len;
if (!random_set.count (val))
Random_set.insert (Val);
}while (Random_set.size () <len);


cout << "Random array between 1 and" << len << ":" << Endl;
Set<int>::iterator set_it = Random_set.begin ();
while (Set_it!= random_set.end ()) {
cout << *set_it << Endl;
++set_it;
}
}


int main (int argc, char** argv) {
Create_rand (100);


return 0;
}


This is the implementation version of PHP:
<?php
function Create_rand () {
$arr = Array ();
do{
$rand = rand (1,100);
if (!in_array ($rand))
$arr [] = $rand;
}while (Count ($arr) <100);


return $arr;
}


echo "Create random integers between 1 and 100:\n";
Var_dump (Create_rand ());
?>






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.