C++ Tutorial/STL Algorithms Modifying sequence operations/unique

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Combine unique and erase to remove elements if there was a previous greater element

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <vector>
  3. include <deque>
  4. include <list>
  5. include <set>
  6. include <map>
  7. include <string>
  8. include <algorithm>
  9. include <iterator>
  10. include <functional>
  11. include <numeric>

/* PRINT_ELEMENTS()

* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/

template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {

   typename T::const_iterator pos;
   std::cout << optcstr;
   for (pos=coll.begin(); pos!=coll.end(); ++pos) {
       std::cout << *pos << " ";
   }
   std::cout << std::endl;

} /* INSERT_ELEMENTS (collection, first, last)

* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/

template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {

   for (int i=first; i<=last; ++i) {
       coll.insert(coll.end(),i);
   }

} using namespace std; int main() {

   // source data
   int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7,
                     5, 4, 4 };
   int sourceNum = sizeof(source)/sizeof(source[0]);
   list<int> coll;
   // initialize coll with elements from source
   copy (source, source+sourceNum,           // source
         coll.begin());                      // destination
   PRINT_ELEMENTS(coll);
   // remove elements if there was a previous greater element
   coll.erase (unique (coll.begin(), coll.end(),
                       greater<int>()),
               coll.end());
   PRINT_ELEMENTS(coll);

}</source>

Use std::unique to eliminate duplicate values

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <algorithm>
  2. include <vector>
  3. include <iterator>

int main() {

  int a1[ 5 ] = { 1, 1, 1, 7, 9 };
  std::vector< int > v1( a1, a1 + 5 );
  std::ostream_iterator< int > output( cout, " " );
  std::copy( v1.begin(), v1.end(), output ); // display vector output
  // eliminate duplicate values
  std::vector< int >::iterator endLocation;
  endLocation = std::unique( v1.begin(), v1.end() );
  cout << endl;
  std::copy( v1.begin(), endLocation, output );
  return 0;

}</source>

1 1 1 7 9
1 7 9

Use unique to remove consecutive duplicates

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <vector>
  3. include <deque>
  4. include <list>
  5. include <set>
  6. include <map>
  7. include <string>
  8. include <algorithm>
  9. include <iterator>
  10. include <functional>
  11. include <numeric>

/* PRINT_ELEMENTS()

* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/

template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {

   typename T::const_iterator pos;
   std::cout << optcstr;
   for (pos=coll.begin(); pos!=coll.end(); ++pos) {
       std::cout << *pos << " ";
   }
   std::cout << std::endl;

} /* INSERT_ELEMENTS (collection, first, last)

* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/

template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {

   for (int i=first; i<=last; ++i) {
       coll.insert(coll.end(),i);
   }

} using namespace std; int main() {

   // source data
   int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7,
                     5, 4, 4 };
   int sourceNum = sizeof(source)/sizeof(source[0]);
   list<int> coll;
   // initialize coll with elements from source
   copy (source, source+sourceNum,           // source
         back_inserter(coll));               // destination
   PRINT_ELEMENTS(coll);
   // remove consecutive duplicates
   list<int>::iterator pos;
   pos = unique (coll.begin(), coll.end());
   /* print elements not removed
    * - use new logical end
    */
   copy (coll.begin(), pos,                  // source
         ostream_iterator<int>(cout," "));   // destination
   cout << "\n\n";

}</source>

1 4 4 6 1 2 2 3 1 6 6 6 5 7 5 4 4
1 4 6 1 2 3 1 6 5 7 5 4