On Feb 21, 4:40 pm, red floyd <no.s...@here.dude> wrote:
> Chris Roth wrote:
> > vector<double> v1(5,1);
> > vector<double> v2;
>
> > v2 = v1; // 1
> > v2.assign(v1.begin(),v1.end()); // 2
>
> > Are 1 and 2 the same, or are their subtle differences between them.
> > Which is preferable, if either? And yes, I know I could use the
> > construction vector<double> v2(v1), but I'm giving an example above.
>
> As John said, there's probably no detectable difference. The latter
> construct (assign) is more useful when you want to copy a subvector.
>
> e.g.:
>
> vector<double> v1;
> vector<double> v2;
>
> // fill v1 here.
>
> vector<double>::iterator start_iter = some_iterator_into_v1;
> vector<double>::iterator end_iter = some_other_iterator_into_v1;
>
> v2.assign(start_iter, end_iter);
Also, using assign allows you to assign across container types:
vector<int> v;
list<int> ll;
// fill ll here
v.assign(ll.begin(), ll.end());
|