C ++ excellent Foundation

Source: Internet
Author: User
Tags acos mathematical constants natural logarithm

Octal: starts with a number 0.
Hexadecimal: starting with 0x or 0x

Q: How to convert strings to different numeric types?
A: general method: sprintf implements number => string
Sscanf string => Number
Other methods: String => the number atoi atof atol converts the string to int, double, and long respectively.
Number => string _ ITOA _ ltoa converts int and long to string,
Here, double => string is special, with _ ECVT _ fcvt _ gcvt
If you use the cstring class of MFC, you can also use the. Format method.

Q: What is the scope and accuracy of various basic data types?
A: type name bytes other names range of values
Int * signed (INT) system dependent
Char 1 signed Char-128 to 127
Long 4 (Signed) Long int-2,147,483,648 to 2,147,483,647
Float 4 none 3.4e +/-38 (7 digits)
Double 8 None 1.7e +/-308 (15 digits)
Long double 10 None 1.2e +/-4932 (19 digits)

Q: About Template
A: The template definition and implementation must be included in the header file! If you write the definition and implementation in
In mytemplate. h and mytemplate. cpp, after # include "mytemplate. H ",
Add # include "mytemplate. cpp"

Q: How do I obtain the time? How accurate?
A: 1 use time_t time (time_t * timer) to be accurate to seconds
2 using clock_t clock (), the CPU time is accurate to 1/clocks_per_sec.
3. Calculate the time difference using double difftime (time_t timer1, time_t timer0)
4 Use DWORD gettickcount () precise to milliseconds
5. If you use the ctime class of MFC, you can use the ctime: rmat method.

Q: What is the scope and accuracy of various basic data types?
A: type name bytes other names range of values
Int * signed (INT) system dependent
Char 1 signed Char-128 to 127
Long 4 (Signed) Long int-2,147,483,648 to 2,147,483,647
Float 4 none 3.4e +/-38 (7 digits)
Double 8 None 1.7e +/-308 (15 digits)
Long double 10 None 1.2e +/-4932 (19 digits)
// Data source: msdn oct2001, but some tests do not match

Q: About Template
A: The template definition and implementation must be included in the header file! If you write the definition and implementation in
In mytemplate. h and mytemplate. cpp, after # include "mytemplate. H ",
Add # include "mytemplate. cpp"

Q: How do I obtain the time? How accurate?
A: 1 use time_t time (time_t * timer) to be accurate to seconds
2 using clock_t clock (), the CPU time is accurate to 1/clocks_per_sec.
3. Calculate the time difference using double difftime (time_t timer1, time_t timer0)
4 Use DWORD gettickcount () precise to milliseconds
5. If you use the ctime class of MFC, you can use ctime: getcurrenttime () to precise to seconds.
6. You can use
Bool queryperformancefrequency (large_integer * lpfrequency)
Obtain the counter frequency of the system.
Bool queryperformancecounter (large_integer * lpperformancecount)
Obtains the counter value.
Then, divide the difference between two counters by frequency to get the time.
7. The methods mentioned in David's article are as follows:
Multimedia Timer Functions
The following functions are used with multimedia timers.
Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime

Q: About Template
A: The template definition and implementation must be included in the header file! If you write the definition and implementation in
In mytemplate. h and mytemplate. cpp, after # include "mytemplate. H ",
Add # include "mytemplate. cpp"

 

Q: How do I obtain the time? How accurate?
A: 1 use time_t time (time_t * timer) to be accurate to seconds
2 using clock_t clock (), the CPU time is accurate to 1/clocks_per_sec.
3. Calculate the time difference using double difftime (time_t timer1, time_t timer0)
4 Use DWORD gettickcount () precise to milliseconds
5. If you use the ctime class of MFC, you can use ctime: getcurrenttime () to precise to seconds.
6. You can use
Bool queryperformancefrequency (large_integer * lpfrequency)
Obtain the counter frequency of the system.
Bool queryperformancecounter (large_integer * lpperformancecount)
Obtains the counter value.
Then, divide the difference between two counters by frequency to get the time.
7. The methods mentioned in David's article are as follows:
Multimedia Timer Functions
The following functions are used with multimedia timers.
Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime
Timegettime/timekillevent/timeproc/timesetevent has high precision

Q: References to pointers and pointers
A: The pointer reference is legal and the referenced pointer is invalid.
Type * & variable must be used for declaration. Type & * variable cannot be used.

Q: questions about random numbers
Q1: how to obtain a random number in a given range?
A1: to obtain a random number in the range [a, B), use (RAND () % (B-a) +.
Q2: Why is the random number sequence obtained by using rand () the same?
A2: rand () generates pseudo-random numbers. Therefore, if you want to generate different sequences each time, you must specify the random number to generate.
Use the void srand (unsigned int seed) function.
Srand (unsign time, which can be used
Bool queryperformancefrequency (large_integer * lpfrequency)
Obtain the counter frequency of the system.
Bool queryperformancecounter (large_integer * lpperformancecount)
Obtains the counter value.
Then, divide the difference between two counters by frequency to get the time.
7. The methods mentioned in David's article are as follows:
Multimedia Timer Functions
The following functions are used with multimedia timers.
Timebeginperiod/timeendperiod/timegetdevcaps/timegetsystemtime
Timegettime/timekillevent/timeproc/timesetevent has high precision

Q: References to pointers and pointers
A: The pointer reference is legal and the referenced pointer is invalid.
Type * & variable must be used for declaration. Type & * variable cannot be used.

Q: questions about random numbers
Q1: how to obtain a random number in a given range?
A1: to obtain a random number in the range [a, B), use (RAND () % (B-a) +.
Q2: Why is the random number sequence obtained by using rand () the same?
A2: rand () generates pseudo-random numbers. Therefore, if you want to generate different sequences each time, you must specify the random number to generate.
Use the void srand (unsigned int seed) function.
Srand (unsigned) Time (null); this method
Q3: How do I get a random floating point number?
Q3: there is no good method. You can try rand ()/double (rand_max) to get 0 ~ Random number between 1

Q: How do I use some mathematical constants?
Q1: pi
A1: Use double to indicate that there are 15 digits, and use long double to indicate that there are 19 digits,
Pi = 3.141592653589793 or const long double Pi = 2 * ACOs (0 );
Q2: E
A2: No, right? Can't you use exp?
Also, log () is the natural logarithm Based on E, and log10 is the base-10 common logarithm.

Q: How does cout control the output format?
A: 1 digit hexadecimal: Hex, Dec, and OCT are used to control the hexadecimal output of numbers.
2 how to align: Use SETW to control the width
3. Set the precision: Use setprecision to control the output precision.
4 Filling Characters: Use setfill to control Filling Characters
5 control format: Use setioflags (ios_base: fmtflags mask) to control
Commonly used IOs: Fixed fixed floating point display
IOS: scientific index Representation
IOS: Left/IOS: Right align left/right
IOS: skipws ignore leading Blank
Signed) Time (null); this method
Q3: How do I get a random floating point number?
Q3: there is no good method. You can try rand ()/double (rand_max) to get 0 ~ Random number between 1

Q: How do I use some mathematical constants?
Q1: pi
A1: Use double to indicate that there are 15 digits, and use long double to indicate that there are 19 digits,
Pi = 3.141592653589793 or const long double Pi = 2 * ACOs (0 );
Q2: E
A2: No, right? Can't you use exp?
Also, log () is the natural logarithm Based on E, and log10 is the base-10 common logarithm.

Q: How does cout control the output format?
A: 1 digit hexadecimal: Hex, Dec, and OCT are used to control the hexadecimal output of numbers.
2 how to align: Use SETW to control the width
3. Set the precision: Use setprecision to control the output precision.
4 Filling Characters: Use setfill to control Filling Characters
5 control format: Use setioflags (ios_base: fmtflags mask) to control
Commonly used IOs: Fixed fixed floating point display
IOS: scientific index Representation
IOS: Left/IOS: Right align left/right
IOS: skipws ignore leading Blank
IOS: uppercase/IOS: lowercase hexadecimal large/lowercase output
Don't forget to add the header file: # include <iomanip. h>

Q: How to create a two-dimensional array?
A: method 1
Type ** arrayname;
Arrayname = new type * [size_1];
For (I = 0; I <size_1; I ++)
{
Arrayname [I] = new type [size_2];
}
Do not forget to release the memory after use:
For (I = 0; I <size_1; I ++)
{
If (arrayname [I]! = NULL) Delete [] arrayname [I];
}
If (arrayname! = NULL) Delete [] arrayname;
Note: When using this method to create an array, if you need to pass the array to the function,
You can pass the pointer of Type **, but if you use type arrayname [] [] to create an array
Regardless of the dimension array, It is a type * pointer. If you try to pass the type * pointer, an error is reported.
Method 2 (recommended)
See the article about dynamic arrays in kirbyzhou.

Q: How to use the plural class?
A: # include <complex>
Using name dimension array?
A: method 1
Type ** arrayname;
Arrayname = new type * [size_1];
For (I = 0; I <size_1; I ++)
{
Arrayname [I] = new type [size_2];
}
Do not forget to release the memory after use:
For (I = 0; I <size_1; I ++)
{
If (arrayname [I]! = NULL) Delete [] arrayname [I];
}
If (arrayname! = NULL) Delete [] arrayname;
Note: When using this method to create an array, if you need to pass the array to the function,
You can pass the pointer of Type **, but if you use type arrayname [] [] to create an array
Regardless of the dimension array, It is a type * pointer. If you try to pass the type * pointer, an error is reported.
Method 2 (recommended)
See the article about dynamic arrays in kirbyzhou.

Q: How to use the plural class?
A: # include <complex>
Using namespace STD;
Complex <type>

Q: Are there any common data structures, such as linked lists, ready for use?
A: There are many things in STL (standard template library,
Such as heap, list, MAP, queue, set, stack, String, vector ......

Q:> and>.
A: For example, vector <pair <cstring, cstring> group is incorrect.
And: vector <pair <cstring, cstring> group is correct.
Regardless of the dimension array, It is a type * pointer. If you try to pass the type * pointer, an error is reported.
Method 2 (recommended)
See the article about dynamic arrays in kirbyzhou.

Q: How to use the plural class?
A: # include <complex>
Using namespace STD;
Complex <type>

Q: Are there any common data structures, such as linked lists, ready for use?
A: There are many things in STL (standard template library,
Such as heap, list, MAP, queue, set, stack, String, vector ......
Using namespace STD;
Complex <type>

Q: Are there any common data structures, such as linked lists, ready for use?
A: There are many things in STL (standard template library,
Such as heap, list, MAP, queue, set, stack, String, vector ......

Q:> and>.
A: For example, vector <pair <cstring, cstring> group is incorrect.
And: vector <pair <cstring, cstring> group is correct.

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.