|
From: Ioannis V. <no...@ya...> - 2002-02-19 12:07:53
|
> -----Original Message-----
> From: dev...@li...
> [ <mailto:dev...@li...>
mailto:dev...@li...] On Behalf
> Of Devin Carless
> Sent: Saturday, February 16, 2002 6:20 PM
> To: dev...@li...
> Subject: Re: [Dev-C++] 'New Source File' bug
>
> >
> > What trouble.
>
> Initializing an array/vector of objects, of which some of the
> members are consts. That was my first problem.
>[...]
> Initializing a vector to anything
> interesting including constants without using push_back() is
> difficult; I ultimately solved the problem by ripping out almost
> every const in my project (not without some satisfaction)
> and using a Set() function. (I implemented a rudimentary
> sort of data protection by only allowing anything to be
> changed if the Name member was set to the constant
> NULLSTR.)
> I've also had problems initializing vectors from arrays.
> Vectors can be initialized in a wonderful panoply of ways,
> all of which are thoroughly useless for my purposes. It's
> recently occurred to me that this is probably a job for an
> overloaded assignment operator. I'm not certain how to
> do this for a vector (while vectors are finicky, templates are
> just plain sticky, and I haven't read much about them
> because I don't expect to need them for a while.)
I can now provide you with more info on this. Consider the code:
#include <vector>
int main()
{
using std::vector;
vector<int>v1(100); // array of 100 ints. Built in types are
initialised to 0
vector<int>v2(100, 1); // array of 100 ints with value 1
v1.assign(10, 20); // Erase the previous elements and place ten 20s.
int array[]={1,2,3,4,5,6,7,8,9,10};
// one past the end
vector<int>v3(&array[0], &array[10]); // Creates v3 having the values
of array
v2=v3;
v1.assign(&array[0], &array[2]); // Make v1 contain 1,2 . v1.size()==2
now
vector<int>v4(v1); // Create an exact copy of v1
}
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
|