"Organizing" unique functions in C + +

Source: Internet
Author: User

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 + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.