How to Implement the Visual component library of C ++ Builder

Source: Internet
Author: User

If you have used a programming language with the string data type, you may not get used to it. Others share the same feeling. Therefore, the standard C ++ Library provides several string operation functions, I hope you can get the information you want in this article.

For detailed descriptions and examples of each function, see the online help of C ++ Builder.

 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

The string operation described here is the string processing method in C language. Most C ++ compilers provide the cstring class to simplify string processing (the C ++ Builder Visual component library contains an AnsiString class that can process string operations. The AnsiString class is described in detail in the online help of C ++ Builder ).

Although the string processing method in C language is troublesome, It is not out of date. C ++ programmers often use the string processing method in C language when using strings such as cstring and AnsiString. Here, we do not want to illustrate every function in the table, but just want to give two of the most commonly used functions. The strcpy () function copies a string to another string. The source string can be a variable or a direct string. For example, the following code:

 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

Here, we create an array of 10 characters, and initially specify a string that requires 9 bytes to remember to terminate null ). Later, I may forget the length of the array. I copied the 16-byte string to the buffer and overloaded the array with six bytes. This small error erased six bytes from a memory location.

Therefore, be careful when copying data to character arrays. Another common string function is sprintf (). This function can combine text and numbers to create a formatted string. The following example adds two numbers and uses sprintf () to create a string to report the results:

 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

In this example, % d tells the sprintf () function that there is an integer value, inserts the variable x at the end of the format string, and tells sprintf () to put the value of variable x at this position of the string. Sprintf () is a special function that can take multiple variable elements. You must provide the target buffer and format string, but the number of variables after the format string is a variable. The sprintf () Example below uses another three yuan:

 
 
  1. //set up a string to hold 29 characters   
  2.  
  3. char buff[30];   
  4.  
  5. //copy a string literal to the buffer   
  6.  
  7. strcpy (buff,"This is a test.");//display it   
  8.  
  9. cout << buff << end;   
  10.  
  11. //initialize a second string buffer   
  12.  
  13. char buff2[]="A second string.";   
  14.  
  15. //copy the contents of this string to the first buffer   
  16.  
  17. strcpy (buff,buff2);   
  18.  
  19. cout << buff << end1;  

Many programmers have forgotten this simple fact and are hard at night. This is a common mistake, not to mention I didn't tell you. C ++ Builder has a brother named wsprintf (), which may be used in Windows sprintf.

Wsprintf () is similar to sprintf (). The only difference is that the floating point number cannot be placed in the format string. Both functions can be used in the C ++ Builder program, but sprintf () is better, because it fully supports floating point numbers and can enter less than one character ). For more information about sprintf (), see the online help of C ++ Builder.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.