alfC
alfC

Reputation: 16272

What is the difference between "vector::operator=" and "vector::assign"?

What is the difference between .operator=(std::initializer_list<T>) and .assign(std::initializer_list<T>) in the std::vector class? Do they do the same?

http://en.cppreference.com/w/cpp/container/vector/operator%3D

http://en.cppreference.com/w/cpp/container/vector/assign

What is the whole idea of having both assign and assigment operator operator=? Is the fact that assign member can accept more than one argument? (However std::initializer_list is a single argument.)

Upvotes: 1

Views: 256

Answers (2)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136415

What is the whole idea of having both assign and assignment operator operator=

You can pass a pair of iterators (range) into assign but not into operator=.

Upvotes: 0

Cubbi
Cubbi

Reputation: 47448

Their return types differ: the operator returns the vector by reference, assign returns void.

Technically, the spec for assignment from initializer_list says the elements are "assigned or destroyed ", while assign from initializer_list "replaces elements " (Table 87), but that seems immaterial: actual implementations of one just call the other (I checked libc++ and libstdc++)

Upvotes: 1

Related Questions