Who can give me a link for the operator= of vector in MSDN?
Why I can only find operator[]?
If operator= is just something default, like copy everything in A to B, how this following code works?
vector<double> v(100,1);
v = vector<double>(200,2); // if operator= is just a trivail version, how to make sure the old v get cleared?
std::vectoris part of the STL, so any description ofstd::vectorwill do. The underlying implementation may differ slightly, but all conformant versions of STL must provide the same interface and the same guarantees forstd::vector. I can’t find in quickly on MSDN, but here are two such descriptions ofoperator=:Now, the second part of your question puzzles me. What would you expect
operator=to do on a vector if not “copy everything from A into B”?In your example, your question was “how to make sure the old v get cleared?”. I can interpret this in one of two ways: You might be asking “how do I know that this assignment
v = vector<double>(200,2)will overwrite the contents ofvand not append to it?”. The answer is that you know that because that’s the definition of howoperator=works for STL containers. Any standards compliant implementation is guaranteed to work that way.You might also be asking “what happens to the contents of
vwhen I overwrite them using the assignmentv = vector<double>(200,2)?” The answer is that they are deallocated, and if they are objects, their destructors are called. Note the important distinction that if the vector contains pointers to objects, the pointers themselves are deallocated, but not what they point to.If I didn’t answer your question, please try to clarify it.