Connecting Tech Pros Worldwide Forums | Help | Site Map

filling vectors in the parameter list

utab
Guest
 
Posts: n/a
#1: Jun 7 '06
Dear all,

I have a function to get some string arguments. The point is that the
number of these string arguments may vary. So

foo(std::vector<std::string> &vec);

But I want to be able to initialize this vector at run-time with
different number of arguments. for instance

foo("str1","str2") // 2 args
foo("str1","str2","str3") // 3 args
foo("str1","str2","str3","str4") // 4 args and so on

I could find the way to put these parameters into the vector. Or is
there a better way to do this kind of task.

Regards


benben
Guest
 
Posts: n/a
#2: Jun 7 '06

re: filling vectors in the parameter list


utab wrote:[color=blue]
> Dear all,
>
> I have a function to get some string arguments. The point is that the
> number of these string arguments may vary. So
>
> foo(std::vector<std::string> &vec);
>
> But I want to be able to initialize this vector at run-time with
> different number of arguments. for instance
>
> foo("str1","str2") // 2 args
> foo("str1","str2","str3") // 3 args
> foo("str1","str2","str3","str4") // 4 args and so on
>
> I could find the way to put these parameters into the vector. Or is
> there a better way to do this kind of task.
>
> Regards
>[/color]

There is a way to declare a function to take varying number of
arguments. However, the way it is done is spectacularly dangerous
because the compiler won't get the type information to protect you.

IMO it is easier to populate the vector first, then pass it to foo:

{
vector<string> v(3);
v.push_back("str1");
v.push_back("str2");
v.push_back("str3");

foo(v);
}

If you find this too verbose you can write a vector_filler<> class
template to fill a vector via an overloaded << operator (like streams do.)

template <typename T, typename A = std::allocator<T> >
class vector_filler
{
std::vector<T, A> v;
public:

const std::vector<T, A>& vec() const{return v;}
std::vector<T,A>& vec(){return v;}

template <typename U>
vector_filler&
operator<<(const U& u){v.push_back(u);}
};

vector_filler<string> vf;
vf << "str1" << "str2" << "str3";

foo(vf.vec());

You may also be tempted to overload operator, (operator comma) but...it
seems it is not without its problems (google them :)

[quasi-off-topic]

I am also under the impression that the next version of C++ standard
(aka C++ 0x) will allow us to initialize a vector just like how we
initialize an array:

vector<string> v = {"str1", "str2", "str3"};
foo(v);

Or

foo({"str1", "str2", "str3"});

I hope it is true.

Regards,
Ben
utab
Guest
 
Posts: n/a
#3: Jun 7 '06

re: filling vectors in the parameter list


[color=blue]
> {
> vector<string> v(3);
> v.push_back("str1");
> v.push_back("str2");
> v.push_back("str3");
>
> foo(v);
> }[/color]

Yes I think this is the easiest but number 3 can change then I can
supply that as a parameter but the problem is that how will I get
number of push_backs.[color=blue]
>
> If you find this too verbose you can write a vector_filler<> class
> template to fill a vector via an overloaded << operator (like streams do.)
>
> template <typename T, typename A = std::allocator<T> >
> class vector_filler
> {
> std::vector<T, A> v;
> public:
>
> const std::vector<T, A>& vec() const{return v;}
> std::vector<T,A>& vec(){return v;}
>
> template <typename U>
> vector_filler&
> operator<<(const U& u){v.push_back(u);}
> };
>
> vector_filler<string> vf;
> vf << "str1" << "str2" << "str3";
>
> foo(vf.vec());
>[/color]
I will examine this one, this is sth more professional[color=blue]
>
> vector<string> v = {"str1", "str2", "str3"};
> foo(v);
>
> Or
>
> foo({"str1", "str2", "str3"});
>
> I hope it is true.
>[/color]

I also hope for that.

Regards

Greg
Guest
 
Posts: n/a
#4: Jun 7 '06

re: filling vectors in the parameter list


benben wrote:[color=blue]
> utab wrote:[color=green]
> > Dear all,
> >
> > I have a function to get some string arguments. The point is that the
> > number of these string arguments may vary. So
> >
> > foo(std::vector<std::string> &vec);
> >
> > But I want to be able to initialize this vector at run-time with
> > different number of arguments. for instance
> >
> > foo("str1","str2") // 2 args
> > foo("str1","str2","str3") // 3 args
> > foo("str1","str2","str3","str4") // 4 args and so on
> >
> > I could find the way to put these parameters into the vector. Or is
> > there a better way to do this kind of task.
> >
> > Regards
> >[/color]
>
> There is a way to declare a function to take varying number of
> arguments. However, the way it is done is spectacularly dangerous
> because the compiler won't get the type information to protect you.
>
> IMO it is easier to populate the vector first, then pass it to foo:
>
> {
> vector<string> v(3);
> v.push_back("str1");
> v.push_back("str2");
> v.push_back("str3");
>
> foo(v);
> }
>
> If you find this too verbose you can write a vector_filler<> class
> template to fill a vector via an overloaded << operator (like streams do.)[/color]

A std::tr1::tuple can also be used as a variable argument list and -
unlike a vector - the arguments do not all have to be of the same type:

#include <tr1/tuple>

#include <string>
#include <iostream>

using std::tr1::tuple;
using std::tr1::make_tuple;
using std::tr1::tie;
using std::string;

void f( tuple< string, int, double> params)
{
string s;
int n;
double d;

// fills in s, n and d
tie( s, n, d) = params;

std::cout << "s: " << s << "\n";
std::cout << "n: " << n << "\n";
std::cout << "d: " << d << "\n";
}

int main()
{
tuple<string, int, double> args( "hello", 5, 2.3 );

f(args);
}

Program output:
s: hello
n: 5
d: 2.3

Greg

mlimber
Guest
 
Posts: n/a
#5: Jun 7 '06

re: filling vectors in the parameter list


benben wrote:[color=blue]
> {
> vector<string> v(3);
> v.push_back("str1");
> v.push_back("str2");
> v.push_back("str3");
>
> foo(v);
> }[/color]

N.B., this vector has 6 strings in it -- three empty, and three not.

Cheers! --M

utab
Guest
 
Posts: n/a
#6: Jun 7 '06

re: filling vectors in the parameter list



[color=blue]
> A std::tr1::tuple can also be used as a variable argument list and -
> unlike a vector - the arguments do not all have to be of the same type:
>
> #include <tr1/tuple>
>
> #include <string>
> #include <iostream>
>
> using std::tr1::tuple;
> using std::tr1::make_tuple;
> using std::tr1::tie;
> using std::string;
>
> void f( tuple< string, int, double> params)
> {
> string s;
> int n;
> double d;
>
> // fills in s, n and d
> tie( s, n, d) = params;
>
> std::cout << "s: " << s << "\n";
> std::cout << "n: " << n << "\n";
> std::cout << "d: " << d << "\n";
> }
>
> int main()
> {
> tuple<string, int, double> args( "hello", 5, 2.3 );
>
> f(args);
> }
>
> Program output:
> s: hello
> n: 5
> d: 2.3
>
> Greg[/color]

Thanks Greg, this can be very helpful.

Regards, and have fun :-))

Closed Thread