I have summed up the erase,lower_bound,upper_bound that I find useful.
- Now, summing up the role of Unique,unique is to "remove" the duplicate elements of adjacent elements in the container (not necessarily requiring an array order), it will add the duplicate elements to the end of the container (so the size of the array does not change), and the return value is the end of the post-weight address, for example.
- Since the return is the end of the container, if you want to remove the size of the weight, you need to subtract the initial address, Lower_bound is to get the address, slightly different.
Such as:
1 1 1);
SZ = unique (a,a + N)-A;
Compare Lower_bound:
1 1, a[i])-B;
- Perhaps you would say that you can simulate directly. By opening an array again, the number is the same as the previous one without adding a new array, just one more array, and soon it's done. So what advantages does unique have? For example, if you want to get a different group of strings adjacent to each other, it is convenient to use unique (as if the simulation is not troublesome, just for "beauty" and use unique bar).
sort (Words.begin (), Words.end ()), Vector<string>::iterator end_unique = Unique ( Words.begin (), Words.end ()); Words.erase (End_unique, Words.end ());
- If you want to delete the repeating element, you can delete the tail (or define the new length directly!). )。
#include <iostream>#include<cassert>#include<algorithm>#include<vector>#include<string>#include<iterator>using namespacestd;intMain () {Const intn= One; intarray1[n]={1,2,0,3,3,0,7,7,7,0,8}; Vector<int>Vector1; for(intI=0; i<n;++i) vector1.push_back (Array1[i]); Vector<int>:: Iterator new_end; New_end=unique (Vector1.begin (), Vector1.end ());//"Delete" adjacent repeating elementsASSERT (vector1.size () = =N); Vector1.erase (New_end,vector1.end ()); //Delete (true delete) duplicate elementsCopy (Vector1.begin (), Vector1.end (),ostream_iterator<int> (cout," ")); cout<<Endl; return 0;}
(The code is of course an exotic product ...) )
"Organizing" unique functions in C + +