c++ - Difference between erase and remove -


I'm a bit confused about the difference between the use of the std :: remove algorithm. Especially I am not able to understand that when I use this algorithm I am writing a small test code that is being removed:

  std :: vector & lt ; Int & gt; a; A.push_back (1); A.push_back (2); Std :: remove (a.begin (), a.end (), 1); Int s = a.size (); Std :: vector & lt; Int & gt; :: Iterator iter = a.begin (); Std :: vector & lt; Int & gt; :: Iterator endIter = a.end (); Std :: cout & lt; & Lt; "Use of Eater ... \ n"; For (; iter! = Antiter; ++ Iter) {std :: cout & lt; & Lt; * Iter & lt; & Lt; "\ N"; } Std :: cout & lt; & Lt; "Use of size ... \ n"; For (int i = 0; i  

In both cases the output was 2,2.

However, if I wipe out something like this:

  a.erase (std :: removal (a.begin (), a.end () , 1), a.end ());  

I get output as 2.

So my questions are:

(1). Whether studi :: is used in addition to experimenting with a trimming work.

(2) Even after std :: removing, why a.size () returns 2 and no 1 returns?

I've read about the sculpture of deletion in the effective STL book of Scott Meyer, but this illusion is still going on.

remove () is not really remove elements from the container - It leaves non-removed elements at the top of the deleted elements. The key is to realize that remove () is designed not only on one container but also for working on any arbitrary forwarding exploiters : which means That not actually remove the elements, because an arbitrary iterator does not have the ability to remove the elements in the pair. For example, there are pointer-forwarded Iterators at the beginning and end of a regular C array, and such as extract () :

  int foo [100]; Can be used with; ... Remove (efu, fu + 100, 42); // Remove all elements equal to 42  

Here it is clear that remove () can not resize the array!


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -