Variable Number of shards

Source: Internet
Author: User
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

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.