この場合、それらは同等です。[および C++03 標準]。ただし、 vectorTwo に代入前の要素が含まれている場合は異なります。それで
vectorTwo = vectorOne; // use operator=
// Any elements held in the container before the call
// are either assigned to or destroyed.
vectorTwo.assign() // any elements held in the container
// before the call are destroyed and replaced by newly
// constructed elements (no assignments of elements take place).
assign
は、operator=
単一の右側オペランドを取るassign
ため、デフォルトの引数値または値の範囲が必要な場合に使用されるため、必要です。最初に適切なベクトルを作成し、次にそれを割り当てることにより、間接的に何assign
ができるでしょうか。
void f(vector<Book>& v, list<Book>& l){
vector<Book> vt = (l.begin(), l.end());
v = vt;
}
ただし、これは見苦しく非効率的です (例は Bjarne Stroustrup "The C++..." から引用されています)。
assign
また、 vector が同じ型でない場合は、暗黙的な変換を許可する whichも必要であることに注意してください。
vector<int> vi;
vector<double> vd;
// ...
vd.assign( vi.begin(), vi.end() );