开发者

Who can give me a link for the operator= of vector in MSDN? [closed]

开发者 https://www.devze.com 2022-12-26 17:34 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question

Who can give me a link for the operator= of vector in MSDN?

Why I can only find operator[]?

开发者_如何学PythonIf 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::vector is part of the STL, so any description of std::vector will do. The underlying implementation may differ slightly, but all conformant versions of STL must provide the same interface and the same guarantees for std::vector. I can't find in quickly on MSDN, but here are two such descriptions of operator=:

  1. http://www.cplusplus.com/reference/stl/vector/operator=/
  2. http://www.sgi.com/tech/stl/Vector.html

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 of v and not append to it?". The answer is that you know that because that's the definition of how operator= 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 v when I overwrite them using the assignment v = 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消