Home c++ function with variable number parameters

# function with variable number parameters

Author

Date

Category

How to transfer the variable number of parameters `x `and one function. From this problem:

Write a feature (or macro definition) that determines whether the point with coordinates (x, y) of the circle with a given radius R. Write a function
Belong with a variable number of parameters, which determines how many points with coordinates (x, y) belong to a given circle. Write the calling function MAIN, which refers to the Belong function at least three times with the number of parameters 3, 9, 11.

Camo Solution The problem is not necessary.

The problem is how to group `X `and `Y `.

We can shove them into a structure, for example `struct point {float x, y; }; `Then the task is reduced to “How to write a function accepting variable number of parameters.” It’s simple, for this there are templates:

``````# include & lt; initializer_list & gt;
#Include & lt; Utility & gt;
STRUCT POINT {FLOAT X, Y; };
TEMPLATE & LT; TYPENAME ... T & GT;
int f (const t & amp; ... args) {
AUTO N = 0;
AUTO R = 10;
For (AUTO & AMP; & amp; P: STD :: INITIALIZER_LIST & LT; Point & GT; {args ...})
if (p.x * p.x + p.y * p.y - r * r & lt; 0.0001)
++ n;
RETURN N;
}
INT MAIN () {
F (Point {11, 12}, Point {22, 23});
}
``````

If we want to transmit coordinates sequentially, i.e. `f (x1, y1, x2, y2) `we can again use templates, but already with recursion:

``````int f () {
Return 0;
}
TEMPLATE & LT; TYPENAME ... T & GT;
Int F (Float X, Float Y, Const T & Amp; ... Tail) {
RETURN INT (X * X + Y * Y - R * R & LT; 0.0001) + F (tail ...);
}
``````

If this is a task from the university, then it is likely that something like this is expected:

``````# include & lt; iostream & gt;
#Include & lt; Cstdarg & gt;
INT MAX (Size_t Number, ...)
{
VA_LIST ArgList;
VA_START (ArgList, Number);
INT MAX = VA_ARG (ArgList, Int);
For (Size_t i = 1; I & LT; Number; ++ I)
{
INT VAL = VA_ARG (ArgList, Int);
Max = STD :: MAX (VAL, MAX);
}
va_end (arglist);
RETURN MAX;
}
INT MAIN ()
{
int maxval = max (6, 12, 43215, 61, 516, 412222, 65125);
STD :: COUT & LT; & LT; "Max Value IS:" & lt; & lt; Maxval & lt; & lt; STD :: ENDL;
Return 0;
}
``````

In the example, the function with a variable number of parameters is used, where the number of parameters is specified first. The decision proposed @abyx is much better, but there is also such that inherited from c.

Variadic Templates do not need to use here. The template magic is something like Kung Fu. Real masters do not use it without need. In addition, in this situation it would be simply incorrectly methodologically. The number of parameters is too large, and the type they have the same. Variadic Templates exist in the language is not defined for such cases. For such cases, there are containers and iterators.

A pleasant bonus when using a container will be the fact that the function for counting the elements that satisfy a certain condition is already in the standard library. All that needs to be done is to write a wrapper for it containing a specific condition.

``````# include & lt; vector & gt;
#Include & lt; algorithm & gt;
Using Namespace STD;
STRUCT POINT {
Float X, Y;
Point (Float x_, float y_): x (x_), y (y_) {}
};
TEMPLATE & LT; TYPENAME IT & GT;
Size_T Belong (IT First, IT Last, Double R) {
RETURN COUNT_IF (FIRST, LAST,
[R] (Const TypeName IT :: REFERENCE ITM) {
RETURN POW (ITM.X, 2) + POW (ITM.Y, 2) -POW (R, 2) & LT; 0.0001;
});
}
INT MAIN ()
{
Vector & lt; Point & GT; POINTS;
Points.Emplace_back (1.0, 1.0);
Points.Emplace_back (2.0, 2.0);
points.Emplace_back (3.0, 3.0);
Size_T Count = Belong (Begin (Points), End (Points), 10);
// Next similar to 9 and 11 points
Return 0;
}
``````

In my opinion, all three answers have “right to life”, given the non-specific issue and the lack of a data source setting.

In the context of the requested language, I, like a respected Shamov, first of all I thought about STL. Because, in real life, the point package would be obtained algorithmically (well, for example, from STDin), and not during compilation, which automatically marks the first and last answer. Accordingly, the container is suitable better. If suddenly embedded case, implement through the transmission of the array of structures.

If, oddly enough, the points are known during compilation, can be templates. Do not forget to pay the size of the application.

If a really university assignment is really a teacher wants to see VA_List 🙂

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.