1

I use two methods when I re-initialize vector

vector<int> vec(1000, 1);

//first
vec.assign(1000, 2);

//second
vec = vector<int>(1000, 3);

I think these two methods are produce same result, but I found that the second method takes up less memory.

Is there something difference between these two method??

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
jsjclink
  • 49
  • 3

1 Answers1

3

The difference is how the internal memory of the std::vector is handled.

With

std::vector<int> vec(1000, 1);

You created a new std::vector called vec. It allocates memory to hold 1000 ints and initializes their values to 1.

In the first method:

vec.assign(1000, 2);

You then tell vec to reuse that allocated memory, where the 1000 ints live. They are simply overwritten by values of 2.

In the second method:

std::vec = std::vector<int>(1000, 3);

You actually do two things - you create a new std::vector which allocates memory for 1000 ints and initializes their values to 3. Then this new vector is move assigned to vec, which in turn will throw away its memory of 2s and take the internal memory of the anonymous unnamed vector with its 3s, because its a so-called rvalue.

The overall memory consumption should be the same in the end, although when using the 2nd method, for a moment you have 2 allocated memory regions of 1000 ints each, alive at the same time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • `std::vector::assign` invalidates iterators because it potentially needs to reallocate, or is there a different reason? – 463035818_is_not_an_ai Sep 02 '20 at 13:48
  • 1
    It could reallocate , or you could also `std::vector::assign` a different i.e. smaller count of integers, which would at least invalidate the past the end iterator. – Superlokkus Sep 02 '20 at 13:52
  • @Superlokkus It always invalidates all of them according to cppreference. Perhaps to let implementors create a new `vector` and just `swap` with that. Otherwise, assigning a smaller count could have just `resize`d and assigned to the elements that would be left, which would leave iterators still valid (except the `end()` iterator - which may be a reason to say they are _all_ invalidated). – Ted Lyngmo Sep 02 '20 at 14:00
  • 2
    @idclev463035818 `std::vector::assign` doesn't "assign" elements (nice name, isn't it). It "replaces" elements. The new elements will be separate objects from the previous elements even though they might share the address. – eerorika Sep 02 '20 at 14:01
  • @eerorika Ooo... "_replace_" ... wording matters. – Ted Lyngmo Sep 02 '20 at 14:03
  • @TedLyngmo I did not say it would not invalidate the other iterators, I was simply ask what other reasons could be, why an implementation would invalidate them, and did not say the list is complete. – Superlokkus Sep 02 '20 at 14:10
  • @Superlokkus Yeah, you said "_at least_" and I just wanted to clarify that it'll invalidate all of them no matter the count. – Ted Lyngmo Sep 02 '20 at 14:12
  • @eerorika I wrote an answer by guessing what `std::vector::asign` does by its name. It was a good leason to learn that names can be extremely misleading. I hope nobody saw that non-sense answer :P – 463035818_is_not_an_ai Sep 02 '20 at 14:14
  • @TedLyngmo Since we both are already showing of on our alleged C++ knowledge? Could you help me out and show me the exact position where the public available standard draft describe the `std::vector::assign` function? I would only find it in the header synopsis but nowhere the according descroption. – Superlokkus Sep 02 '20 at 14:25
  • "_Since we both are already showing of on our alleged C++ knowledge_" - I wasn't aiming to show off - merely to clarify something that could _perhaps_ be misunderstood. – Ted Lyngmo Sep 02 '20 at 14:28
  • @TedLyngmo I was hoping you would let me get away with this;-). But seriously oddly I cant not find the actual definition of `std::vector::assign` or `std::deque::assign` besides their mere declaration in the header synopsis. – Superlokkus Sep 02 '20 at 14:31
  • @Superlokkus Neither can I :-) Cheers! – Ted Lyngmo Sep 02 '20 at 14:32
  • 2
    @Superlokkus Or perhaps here it is: http://eel.is/c++draft/sequence.reqmts#4 – Ted Lyngmo Sep 02 '20 at 14:38
  • 1
    @TedLyngmo Thank you, because searching "void assign" would not find "assign void" as they wrote it, I was almost going to submit a defect report. Thanks for saving me from the shame :-) – Superlokkus Sep 02 '20 at 14:48