A
Andreas Krueger
Hi!
I am fed up with
vector<int> iv;
iv.push_back(42); iv.push_back(9); iv.push_back(11); ...
and would rather use a function "fillVector":
vector<int> iv = fillVector(42,9,11);
like in Mathematica(tm): someList={42,9,11};
or even some2dimList={{42},{9,11}};
But there are some difficulties with it.
Perhaps you have good ideas how to solve it?
The sample code is below.
If you don't like templates, just leave out template<class T>
and replace every single "T" by "int"
a) In Visual C++ 6 the sample code works WITHOUT an ending 0:
25 vector<int> intvec=fillVector(800,500,200,300,400);
but in gcc 2.95.2 the last parameter HAS TO BE 0:
25 vector<int> intvec=fillVector(800,500,200,300,400,0);
otherwise the for(; breaks much later.
Moreover, even in Visual C++ 6, the problem occurs with
un-0-terminated lists when I use the vector result as
a parameter for some other function.
(see line 33) - so probably it's just by chance that it
works if I use an intermediate storage vector as in line 25
So: Is there any other way to notice the end (or length)
of a va_list ??? so that the list of parameters doesn't
need to be 0-terminated?
b) It works with "int" but not with "string"
because there doesn't seem to be a "0" in string - or is there?
compiler reports problem in line 15 because of (p==0)
even ( p == (T) 0 ) or a ( p == T(0) ) doesn't help.
any ideas concerning the "0" in string?
c) Any ideas for 2(3)dimensional arrays?
vector<vector<int> > intMatrix=fillVector((800,500),(200,300),(400,12));
Please answer not only to the list but also to my email...
Many thanks in advance!
Andreas
sample code:
1 #include <vector>
2 #include <algorithm>
3 #include <iostream>
4 using namespace std;
5 #pragma warning(disable:4786)
6
7 #include <cstdarg>
8 template<class T> vector<T> fillVector(T noZeroes ...){
9 vector<T> theVector;
10 theVector.push_back(noZeroes);
11 va_list ap;
12 va_start(ap, noZeroes);
13 for (;{
14 T p = va_arg(ap, T);
15 if (p==0 ) break;
16 theVector.push_back(p);
17 }
18 va_end(ap);
19 return theVector;
20 } // idea from chapter 7.6 of Stroustrup, 3rd Edition
21
22 void print_tab(int i) { cout<<i<<"\t"; };
23 void someFunction(vector<int> v){
for_each(v.begin(),v.end(),print_tab);
};
24
25 int main (){
26 vector<int> intvec=fillVector(800,500,200,300,400,0);
27 for_each(intvec.begin(),intvec.end(),print_tab);
28 cout<<"\n";
29
30 // vector<string> stringvec
=fillVector<string > ("Andreas","Test","Schubbidu","");
31 // for_each(stringvec.begin(),stringvec.end(),print_tab);
32
33 someFunction(fillVector(800,500,200,300,400));
34 return 0;
35 }
I am fed up with
vector<int> iv;
iv.push_back(42); iv.push_back(9); iv.push_back(11); ...
and would rather use a function "fillVector":
vector<int> iv = fillVector(42,9,11);
like in Mathematica(tm): someList={42,9,11};
or even some2dimList={{42},{9,11}};
But there are some difficulties with it.
Perhaps you have good ideas how to solve it?
The sample code is below.
If you don't like templates, just leave out template<class T>
and replace every single "T" by "int"
a) In Visual C++ 6 the sample code works WITHOUT an ending 0:
25 vector<int> intvec=fillVector(800,500,200,300,400);
but in gcc 2.95.2 the last parameter HAS TO BE 0:
25 vector<int> intvec=fillVector(800,500,200,300,400,0);
otherwise the for(; breaks much later.
Moreover, even in Visual C++ 6, the problem occurs with
un-0-terminated lists when I use the vector result as
a parameter for some other function.
(see line 33) - so probably it's just by chance that it
works if I use an intermediate storage vector as in line 25
So: Is there any other way to notice the end (or length)
of a va_list ??? so that the list of parameters doesn't
need to be 0-terminated?
b) It works with "int" but not with "string"
because there doesn't seem to be a "0" in string - or is there?
compiler reports problem in line 15 because of (p==0)
even ( p == (T) 0 ) or a ( p == T(0) ) doesn't help.
any ideas concerning the "0" in string?
c) Any ideas for 2(3)dimensional arrays?
vector<vector<int> > intMatrix=fillVector((800,500),(200,300),(400,12));
Please answer not only to the list but also to my email...
Many thanks in advance!
Andreas
sample code:
1 #include <vector>
2 #include <algorithm>
3 #include <iostream>
4 using namespace std;
5 #pragma warning(disable:4786)
6
7 #include <cstdarg>
8 template<class T> vector<T> fillVector(T noZeroes ...){
9 vector<T> theVector;
10 theVector.push_back(noZeroes);
11 va_list ap;
12 va_start(ap, noZeroes);
13 for (;{
14 T p = va_arg(ap, T);
15 if (p==0 ) break;
16 theVector.push_back(p);
17 }
18 va_end(ap);
19 return theVector;
20 } // idea from chapter 7.6 of Stroustrup, 3rd Edition
21
22 void print_tab(int i) { cout<<i<<"\t"; };
23 void someFunction(vector<int> v){
for_each(v.begin(),v.end(),print_tab);
};
24
25 int main (){
26 vector<int> intvec=fillVector(800,500,200,300,400,0);
27 for_each(intvec.begin(),intvec.end(),print_tab);
28 cout<<"\n";
29
30 // vector<string> stringvec
=fillVector<string > ("Andreas","Test","Schubbidu","");
31 // for_each(stringvec.begin(),stringvec.end(),print_tab);
32
33 someFunction(fillVector(800,500,200,300,400));
34 return 0;
35 }