When the number of workers is passed in C ++, sometimes the number of workers is not determined. In this case, the variable number of workers can be used. Example: 1. STD: initializer_list
C ++ 11 standard. The usage method is similar to that of vector, and the number of bytes type must be the same. The usage is as follows:
# Include <initializer_list>
Void method (STD: initializer_list <int> il)
{
For (Auto I: il)
{
Printf ("% d", I );
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Method ({1, 3, 4, 6, 7 });
Return 0;
}
2. Omitting the operator
The Escape Character is set to facilitate the C ++ program pipeline to ask some special C code, which uses the C standard library function named varargs. Generally, the omitting operator should not be used for other purposes.
SLAVE: the omitting operator should only be used for C and C ++ general types. Note that most objects of the class type cannot be copied correctly when they are passed to the omitted delimiter.
The Omitting operator must be at the end of the number of operators list, and there must be at least one prior operator. The usage is as follows:
# Include <stdarg. h>
Void method (int I ,...)
{
Int V;
Va_list Params; // declare a list of Variable Arguments
Va_start (Params, I); // obtain the starting address of the Variable Arguments based on the arguments before the variable arguments.
Do {
V = va_arg (Params, INT); // obtain the value of the number of arguments from the variable sequence table. The type is int.
Printf ("% d", V );
} While (V! = 0) // you need to set the ending mark of the Variable Number list, that is, when you read a value, it indicates that the variable number list reaches the end, if no ending standard is set, the system will continue to read the memory data, and unexpected problems may occur.
Va_end (Params); // end
}
// Format the string using the omitted character-shaped escape
Void format (const char * format ,...)
{
Char Buf [1024];
Va_list Params;
Va_start (Params, format );
Vsnprintf_s (BUF, 1024,102 4, format, Params );
Va_end (Params );
Printf ("% s", Buf );
}
3. variable number of macros
# Define format (format,...) format (format, ##__va_args _) // use #__ va_args _ to pass the variable number of arguments
Int _ tmain (INT argc, _ tchar * argv [])
{
Method (1, 3, 4, 6, 7, 0 );
Format ("int: % d, string: % s, float: % F", 10, "test", 1.23 );
Format ("int: % d, string: % s, float: % F", 10, "test", 1.23 );
Return 0;
}
Variable Number of shards