11 National Day C + + Small summary __c++

Source: Internet
Author: User
Tags flushes
1. Find characters from string:


Char *strchr (const char *s,int c);
Description: This function looks for the character with a C value from the first character in the string of S, and returns the stored address of the character if the lookup succeeds, or returns Null (that is, the value 0).
When the function is called newsletters to the argument of the second parameter C, it can be an integer, but is usually a character to be looked for.
Example: STRCHR ("ABCD", ' C ') returns "CD"
STRCHR ("ABCDEFG", ' C ') returns "CDEFG"


2. Find characters in reverse order from string:


Char *strrchr (const char *s,int c);
Description: It is the same as the STRCHR function described above, is to look up characters from a string, but the lookup order is different from the last character of the string that s refers to, and if you look for a stored address that returns characters successfully, return NULL.
Example: STRCHR ("Abcdce", ' C ') returns "Cdce"
If STRRCHR ("Abcdce", "C") returns "CE"


Note: After your own test, call two functions, the value of the original string has not changed, still "ABCDCE"


3. Find substring from string


Char *strstr (const char *s1,const char *S2);
Description: This function returns the same substring as the string of the second parameter S2, starting with the first character in the string S1 the first argument, or null if the lookup succeeds.
Example: Strstr ("Abcdce", "CD") returns "Cdce"




4. Sometimes you want the elements in the container to be traversed in reverse order. If the container has a bidirectional iterator, the following code may be written:
Vector<int>::iterator p;
for (P = container.end (); P!= Container.begin (); p--)
cout << *p << Endl;
The code above can be compiled successfully, and you can actually get the results you want on some systems. (Note: Tests on my machine can be compiled and in reverse order, but the first element is not printed)
Fortunately there is a very simple way to meet the requirements. For containers with bidirectional iterators, the so-called reverse iterators (reverse iterator) can be used to turn everything back. The following code works well:
Vector<int>::reverse_iterator RP;
for (RP = Container.rbegin (); RP!= Container.rend (); rp++)
cout << *rp << Endl;


5. It is best to declare a destructor of a base class as a virtual function. This will automatically make the destructor of all derived classes a virtual function. (constructors cannot be declared as virtual functions.) This is because the class object did not complete the build process when the constructor was executed, and of course it was not bound to the class object.


6. In C, corresponding to a string pointer, to output the string itself or a pointer, we can achieve different output directly in a different format. such as ("%p") or ("%s")


and in C + +, if we direct cout << s<< Endl; The output is the string itself, and to output the pointer address, we can force the type to be converted to void *, i.e. cout << (void *) s << Endl;
Cases:
char * s = "Chenyong";
cout << s << endl; ----> will output Chenyong
cout << (void *) s << Endl; ----> Output Pointer address


7.cout << "Hello World" << endl---> Will output Hello World
cout << cout << Endl; ---> Will output cout address
cout << "Hello World" << cout << "Ni hao"; ---> Will output Hello World +cout address + ni Hao
cout << "Hello World" << "Ni Hao"; ---> Will output Hello World ni hao


8. In addition to the various operator<< () functions, the Ostream class also provides a put () method and a write () method for displaying characters, which is used to display strings. Cases:
Cout.put (' W '); ---> Display character W (now C + + supports direct cout << ' W ' to output characters.)


The first parameter of write () provides the address of the string to display, and the second argument indicates how many characters to display:
const char *state = "Kansas";
for (int i = 0; I <= strlen (state); i++)
Cout.write (state,i) <<endl;


The output
K
Ka
Kan
Kans
Kansa
Kansas


9. About the cout buffer:
cout << "Enter a number:";
while (1);
The upper string will not be exported to the screen, which involves the knowledge of the buffer:
Because the Ostream class buffers the output of the Cout object processing, the output is not immediately sent to the destination address, but is stored in the buffer until the buffer is full. The program then refreshes (flush) The buffer, sends the content out, and empties the buffer to store the new data. Typically, the buffer is 512 bytes or its integer times. Buffering can save a lot of time when standard output is connected to a file on a hard disk. However, for screen output, the buffer is much less important first, so there are several ways to flush the buffer, such as when a newline character is sent to the buffer. And also:
cout << "Enter a number:";
float num;
CIN >> Num;
The fact that the program expects to enter this will cause it to immediately display the cout message, even if there is no line break in the output string (note that the previous example distinguishes)
There are two functions that enable forced flushes: flush and Endl. The control character flush flushes the buffer, while the controller Endl flushes the buffer and inserts a newline character.
In fact, the control character is also a function, for example, you can call flush () directly to flush the buffer:
Flush (cout); or cout << flush;


10. In the same program, multiple such forms can occur at the same time:
for (int i = 0; i < N; i++) {}
for (int i = 0; i < M i++) {}---that is: All can be named with the same name, because it is local (from): I tried, I wouldn't warn you.
Related Article

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.