FAQ Entry
Mark Mitchell
mmitchell@usa.net
Tue Jan 6 23:27:00 GMT 1998
Jeff & Joe --
Here's another entry for the egcs FAQ, which I thought Joe might
want in his FAQ as well:
Q. I can say:
#include <vector>
...
vector<double> vd(10, 3.0);
to create a vector of 10 doubles, each initialized to 3.0, but
I can't say:
#include <vector>
...
vector<int> vi(10, 3);
to create a vector of 10 ints, each initialized to 3. Why not?
A. There are two constructors of interest for vector, as specified
in the Standard:
template <class InputIterator>
vector<InputIterator, InputIterator);
vector(size_type, const T&);
By the rules given in the Stanard, the best match for the
arguments 10 and 3 is the InputIterator version, even though
10 and 3 are not, of course, iterators. So, the code doesn't work
as expected. You can write:
vector<int> vi((vector<int>::size_type) 10, 3);
or, since the size_type is unsigned,
vector<int> vi(10U, 3);
to do what you expect.
--
Mark Mitchell mmitchell@usa.net
Stanford University http://www.stanford.edu
More information about the Gcc
mailing list