473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to Accept Variable number of arguments ?

Trying to write a function which can accept variable number of
arguments of the same data type , ranging from 1 ... n . What would be
the best way to go about this.

Thanks,
vivekian

Nov 24 '05 #1
7 1699
If all the arguments are going to be of the same type, the easiest way
all around is just to pass in a vector of that type. You'll then be
able to iterate over all the inputs. If whatever you're doing can be
generalized, you could template the function to take a vector<T> too.

Nov 24 '05 #2
I believe the elipses operator is what you want here. printf uses it.

void someMethod(int number_of_arguments, ...)
{

}

This will allow you to take any number of arguments, of any type. Beware it
can easily be abused, and should be used with caution (the elipses operator,
not printf.)

"vivekian" <vi********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Trying to write a function which can accept variable number of
arguments of the same data type , ranging from 1 ... n . What would be
the best way to go about this.

Thanks,
vivekian

Nov 24 '05 #3
vivekian wrote:
Trying to write a function which can accept variable number of
arguments of the same data type , ranging from 1 ... n . What would be
the best way to go about this.


One way is to ose the va_* macros declared in <stdarg.h>.

It's impossible to say what the best way is unless you tell us what kind
of function you are trying to write. It all depends on exactly what you
are trying to do.

john
Nov 24 '05 #4
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.

Thanks again,
vivekian

Nov 24 '05 #5

vivekian wrote:
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.


You don't need a function for that! The constructor of std::list< >
already
does that.

HTH,
Michiel Salters

Nov 24 '05 #6
In message <11**********************@g49g2000cwa.googlegroups .com>,
vivekian <vi********@gmail.com> writes
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.

If you're adding elements one at a time, you might consider overloading
operator<<:

// warning - incomplete skeleton code for illustration only
class MyType {
public:
void add(int i);
// etc...
};

MyType & operator<<(MyType & obj, int arg)
{
obj.add(arg);
return obj;
}

MyType x;
x << arg1 << arg2 << arg3; // etc

If your set of arguments constitute some kind of a sequence, you could
have a templated function that takes two iterators representing the
first and one-beyond-the end elements. This is similar to meagar's
solution, but more flexible, since you aren't constrained to pass a
vector, but can use any kind of representation that behaves like an
input iterator - for example you could use an istream_iterator to read
the data from a file.

template <typename Iterator>
void AddElements(MyType & obj, Iterator p, Iterator end)
{
for (; p!=end; ++p)
obj.add(*p);
}

MyType x;
AddElements(x, istream_iterator<int>(cin), istream_iterator<int>());

In this case you could alternatively make this a member function of
MyType and omit the first argument.

--
Richard Herring
Nov 24 '05 #7
Thanks for all the solutions . Think Richards solution suits my problem
the best and works well.

vivekian

Nov 25 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
2
by: PengYu.UT | last post by:
I'm wondering whether the operator can accept more than 1 arguments Suppose I have a object which is essentially a 2 dimensional array, I want to use operator to access the data. I don't what...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
16
by: utab | last post by:
Dear all, How can I generate a constructor to take a variable number of arguments. Is boost tuple appropriate for this? I know that there is a way to make functions take variable number of...
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.