Unique and unique_copy of STL algorithm

Source: Internet
Author: User

I. Unique function

The class attribute algorithm has the unique function of "delete" all adjacent repeating elements from the input sequence.

The algorithm deletes the adjacent repeating elements, then rearranges the elements within the input range, and returns an iterator (the length of the container is unchanged, but the order of elements is changed), indicating that the range of values without duplicates ends.

1//Sort words alphabetically so we can find the Duplicates 
 2 sort (words.begin (), Words.end ()); 
 3      /* Eliminate duplicate words: 
 4       * Unique reorders words so this all word appears once in the 
 5       *
  front portion of words and returns an iterator one past the 
 6 unique range; 
 7       * Erase uses a vector operation to remove the nonunique elements 
 8       */ 
 9  Vector<string> : iterator End_unique =  unique (words.begin (), Words.end ());  words.erase (End_unique, Words.end ());
In the STL, the unique function is a function of a heavy the unique function is to remove the adjacent repeating element (only one), in fact it does not really delete the duplicate elements, is to move the repeating element to the back, and then still save to the original array, and then return to the end of the last element after the address, Because the unique removal is adjacent to the repeating element, so generally used before will have to arrange the order.

If you call sort, the elements of the vector's objects are sorted in order:

Sort  jumps over  quick  red  Red  slow  the  turtle

When unique is invoked, the content stored in the vector is:

Note that the size of the words has not changed, and still holds 10 elements, but the order of these elements has changed. Calls the unique "delete" of adjacent duplicate values. Add quotes to delete because the unique does not actually delete any elements, but instead copies the elements without duplicates to the previous segment of the sequence, overwriting the adjacent repeating elements. The unique returned iterator points to the next position beyond the end of the range of elements without duplicates.

Note: The algorithm does not directly modify the size of the container. If you need to add or remove elements, you must use a container action.

Example

1 #include <iostream>
 2 #include <cassert>
 3 #include <algorithm>
 4 #include <vector >
 5 #include <string>
 6 #include <iterator>
 7  using namespace std;
 8 
 9  int main ()
{one     //cout<< "illustrating the generic unique algorithm." <<endl;
A     const int n=11;     int array1[n]={1,2,0,3,3,0,7,7,7,0,8};     vector<int> Vector1;
for     (int i=0;i<n;++i)         vector1.push_back (Array1[i]);     vector<int>::iterator new_end;     New_end=unique (Vector1.begin (), Vector1.end ());    "Delete" adjacent repeating element     assert (Vector1.size () ==n);     vector1.erase (New_end,vector1.end ());  Delete (real deletion) duplicate elements (     vector1.begin (), Vector1.end (),ostream_iterator<int> (cout, ""));     cout<<endl;     0;
27}

The results of the operation are:

Second, unique_copy function

The algorithm standard library defines a function called Unique_copy, which operates like a unique one.

The only difference is that the former accepts the third iterator argument, which specifies the target sequence for copying the distinct elements.

Unique_copy literally means to remove the repeating element and then perform the copy operation.

The writer uses unique_copy to assign a distinct element in a list object to an empty vector object.

1//using the unique_copy algorithm
 2//Assign a value that is not duplicated in a list object to an empty vector object
 3 #include <iostream>
 4 #include <list >
 5 #include <vector>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 int main ()
{one     int ia[7] = {5, 2, 2, 2, 5, 2};     list<int> Ilst (IA, IA + 7);     vector<int> Ivec;     //Copy the ILST elements from the list object to the empty vector object Ivec     //sort (Ilst.begin (), Ilst.end ());  Cannot use this sort, will error     ilst.sort ();  To sort before copying, remember     unique_copy (Ilst.begin (), Ilst.end (), Back_inserter (Ivec));     output vector container     cout<< "vector:" <<endl;
For     (Vector<int>::iterator iter = Ivec.begin (); Iter!= ivec.end (); ++iter)         cout<<* iter<< "";     cout<<endl;     0;
27}

If

List<int> Ilst (IA, IA + 7);
instead:vector<int> Ilst (IA, IA + 7);

is available when sorting:

Sort (Ilst.begin (), Ilst.end ());

Notice how the list and vector are sorted.

These words may be useful in the effective STL:
item
"Let's summarize your sorting choices:
If you need to do a complete sort of vector, string, deque, or array, You can use sort or stable_sort.
If you have a vector, string, deque, or array, you only need to sort the first n elements, and you should use Partial_sort.
If you have a vector, string, deque, or array, you need to identify the nth element or you need to identify the first n elements without knowing their order, nth_element is what you should notice and call.
if you need to separate the elements or arrays of a standard sequence container into satisfying and not satisfying a standard, you'll probably want to find partition or stable_partition.
If your data is in the list, you can use partition and stable_partition directly, and you can use the sort of list instead of sort and stable_sort. If you need Partial_sort or nth_element to provide the effect, you must do it indirectly, but as I outlined above, there will be many choices. In
addition, you can keep everything in order at all times by putting the data in a standard associative container. You may also consider standard non-STL container priority_queue, and it can always keep its elements in order.    
Category: C + + STL

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.