C++ Tutorial/STL Algorithms Modifying sequence operations/unique copy
Содержание
- 1 Copy only unique elements in a vector into another vector
- 2 Copy standard input to standard output while compressing spaces
- 3 Read all characters while compressing whitespaces
- 4 Remove adjacent duplicates: unique_copy
- 5 Use unique_copy to copy elements in an array to list with back_inserter
- 6 Use unique_copy to print elements with consecutive duplicates removed
- 7 Use unique_copy to print elements without consecutive entries that differ by one
Copy only unique elements in a vector into another vector
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <algorithm>
- include <vector>
- include <iterator>
int main() {
const int SIZE = 10; int a1[ SIZE ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 }; std::vector< int > v1( a1, a1 + SIZE ); // copy of a std::ostream_iterator< int > output( cout, " " ); cout << "Vector v1 contains: "; std::copy( v1.begin(), v1.end(), output ); std::vector< int > results1; std::unique_copy( v1.begin(), v1.end(), std::back_inserter( results1 ) ); cout << "\nAfter unique_copy results1 contains: "; std::copy( results1.begin(), results1.end(), output ); return 0;
}</source>
Vector v1 contains: 1 3 5 7 9 1 3 5 7 9 After unique_copy results1 contains: 1 3 5 7 9 1 3 5 7 9
Copy standard input to standard output while compressing spaces
<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. */
- include <iostream>
- include <algorithm>
- include <iterator>
using namespace std; bool bothSpaces (char elem1, char elem2) {
return elem1 == " " && elem2 == " ";
} int main() {
// don"t skip leading whitespaces by default cin.unsetf(ios::skipws); /* copy standard input to standard output * - while compressing spaces */ unique_copy(istream_iterator<char>(cin), // beginning of source: cin istream_iterator<char>(), // end of source: end-of-file ostream_iterator<char>(cout), // destination: cout bothSpaces); // duplicate criterion
}</source>
a a Terminate batch job (Y/N)? n
Read all characters while compressing whitespaces
<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. */
- include <iostream>
- include <string>
- include <algorithm>
- include <iterator>
- include <locale>
using namespace std; class bothWhiteSpaces {
private: const locale& loc; // locale public: /* constructor * - save the locale object */ bothWhiteSpaces (const locale& l) : loc(l) { } /* function call * - returns whether both characters are whitespaces */ bool operator() (char elem1, char elem2) { return isspace(elem1,loc) && isspace(elem2,loc); }
}; int main() {
string contents; // don"t skip leading whitespaces cin.unsetf (ios::skipws); //Read all characters while compressing whitespaces unique_copy(istream_iterator<char>(cin), // beginning of source istream_iterator<char>(), // end of source back_inserter(contents), // destination bothWhiteSpaces(cin.getloc())); // criterion for removing // process contents // - here: write it to the standard output cout << contents;
}</source>
string string asdf string string asdf Terminate batch job (Y/N)? n
Remove adjacent duplicates: unique_copy
<source lang="cpp">#include <string>
- include <iostream>
- include <algorithm>
using namespace std; int main() {
const string hello("Hello, how are you?"); string s(hello.begin(),hello.end()); // iterate through all of the characters string::iterator pos; for (pos = s.begin(); pos != s.end(); ++pos) { cout << *pos; } cout << endl; /* remove adjacent duplicates * - unique() reorders and returns new end * - erase() shrinks accordingly */ s.erase (unique(s.begin(), s.end()), s.end()); cout << "no duplicates: " << s << endl;
}</source>
Hello, how are you? no duplicates: Helo, how are you?
Use unique_copy to copy elements in an array to list with back_inserter
<source lang="cpp">#include <iostream>
- include <list>
- include <algorithm>
- include <string>
using namespace std; int main( ) {
string arrStr[5] = {"A", "B", "A", "D", "E"}; list<string> lstStrDest; unique_copy(&arrStr[0], &arrStr[5], back_inserter(lstStrDest)); for (list<string>::iterator p = lstStrDest.begin( ); p != lstStrDest.end( ); ++p) { cout << *p << endl; }
}</source>
A B A D E
Use unique_copy to print elements with consecutive duplicates removed
<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. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- 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; bool differenceOne (int elem1, int elem2) {
return elem1 + 1 == elem2 || elem1 - 1 == elem2;
} 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]); // initialize coll with elements from source list<int> coll; copy(source, source+sourceNum, // source back_inserter(coll)); // destination PRINT_ELEMENTS(coll); // print elements with consecutive duplicates removed unique_copy(coll.begin(), coll.end(), // source ostream_iterator<int>(cout," ")); // destination cout << endl;
}</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
Use unique_copy to print elements without consecutive entries that differ by one
<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. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- 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; bool differenceOne (int elem1, int elem2) {
return elem1 + 1 == elem2 || elem1 - 1 == elem2;
} 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]); // initialize coll with elements from source list<int> coll; copy(source, source+sourceNum, // source back_inserter(coll)); // destination PRINT_ELEMENTS(coll); // print elements without consecutive entries that differ by one unique_copy(coll.begin(), coll.end(), // source ostream_iterator<int>(cout," "), // destination differenceOne); // duplicates criterion cout << endl;
}</source>
1 4 4 6 1 2 2 3 1 6 6 6 5 7 5 4 4 1 4 4 6 1 3 1 6 6 6 4 4