The format function in C ++ builder and its usage.

Source: Internet
Author: User
The format function in C ++ builder serves the same purpose as that in Delphi. But the only difference is the parameter. Because delphi supports the "on Parameters" and dynamic array features, the format in Delphi only has two parameters, the latter of which is a dynamic open parameter constant array. Pay attention to my words! The type declaration of Delphi's dynamic open parameter constant array is array of Const. Each element of this dynamic constant open array can be of different types. In addition, the syntax used is similar to the set in Delphi: two square brackets are used to enclose different types of variables, you can also declare an array of the tvarrec type to accommodate different types of variables (see its help documentation for details ). In C ++ builder, there are no syntax features like those in Delphi. The first parameter of the format function in BCB is similar to that in Delphi format (a formatting description string ), the following two parameters are provided. The second parameter is an array pointer of the tvarrec type. The third parameter is the index value of the last element of the array! Because c ++ does not support dynamic array syntax and does not have any weird features such as "Open Parameters", when you pass an array, you must pass the size of the array at the same time (format requires the index value of the last element of the array, so it is similar ). As I said earlier, in Delphi, we can also use the tvarrec array to replace square brackets. In essence, they are the same. Of course, in C ++ builder, we do not need to declare a tvarrec array every time we use the format function, and then assign the variables to be used for output to every element of the tvarrec array, finally, the index of the last element of the tvarrec array is passed. In fact, we can use a pre-declared macro arrayofconst in C ++ builder to directly input the variable to be formatted and output. See the following example:

Memo1-> text = format (
"My name is % s, I'm % d years old .",
Arrayofconst ("phoenix2000", 22) // note the two parentheses
);

OK, everything is done! The essence of this macro is to pass two parameters to the format function. One is the tvarrec number two pointer, and the other is the index of the last element of the number two. You can refer to the help documentation. In addition, the ansistring class in C ++ builder also has a format class member function. However, this member function is a static function. Pay attention to it when using it.

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.