The main reason for using assign
is to copy data from one type of container to another.
For example, if you want to migrate the contents of an std::set<int>
to an std::vector<int>
, you can't use the assignment operator, but you can use vector.assign(set.begin(), set.end())
.
Another example would be copying the contents of two containers holding different types that are convertible to one or the other; If you try to assign std::vector<Derived*>
to an std::vector<Base*>
, the assignment operator is insufficient.