C++ Tutorial/STL Algorithms Modifying sequence operations/remove
Содержание
- 1 Combine remove and erase together
- 2 Remove an element and then erase that element
- 3 Remove value from a vector with remove()
- 4 std::remove does not change the size of the container,it moves elements forward to fill gaps created and returns the new "end" position.
- 5 Use remove() to delete elements from a vector
- 6 Use std::remove to delete all element in a vector by value
- 7 Use the generic remove algorithm
Combine remove and erase together
<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; int main() {
vector<int> coll; INSERT_ELEMENTS(coll,2,6); INSERT_ELEMENTS(coll,4,9); INSERT_ELEMENTS(coll,1,7); PRINT_ELEMENTS(coll,"coll: "); // remove all elements with value 5 vector<int>::iterator pos; // remove all elements less than 4 coll.erase(remove_if(coll.begin(), coll.end(), // range bind2nd(less<int>(),4)), // remove criterion coll.end()); PRINT_ELEMENTS(coll,"<4 removed: ");
}</source>
coll: 2 3 4 5 6 4 5 6 7 8 9 1 2 3 4 5 6 7 <4 removed: 4 5 6 4 5 6 7 8 9 4 5 6 7
Remove an element and then erase that 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. */
- 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; int main() {
vector<int> coll; INSERT_ELEMENTS(coll,2,6); INSERT_ELEMENTS(coll,4,9); INSERT_ELEMENTS(coll,1,7); PRINT_ELEMENTS(coll,"coll: "); // remove all elements with value 5 vector<int>::iterator pos; pos = remove(coll.begin(), coll.end(), // range 5); // value to remove PRINT_ELEMENTS(coll,"size not changed: "); // erase the ""removed"" elements in the container coll.erase(pos, coll.end()); PRINT_ELEMENTS(coll,"size changed: ");
}</source>
coll: 2 3 4 5 6 4 5 6 7 8 9 1 2 3 4 5 6 7 size not changed: 2 3 4 6 4 6 7 8 9 1 2 3 4 6 7 5 6 7 size changed: 2 3 4 6 4 6 7 8 9 1 2 3 4 6 7
Remove value from a vector with remove()
<source lang="cpp">#include <iostream>
- include <algorithm>
- include <vector>
using namespace std; bool greater9( int ); int main() {
const int SIZE = 10; int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; // Remove 10 from v vector< int > v( a, a + SIZE ); vector< int >::iterator newLastElement; newLastElement = remove( v.begin(), v.end(), 10 ); return 0;
} bool greater9( int x ) {
return x > 9;
}</source>
std::remove does not change the size of the container,it moves elements forward to fill gaps created and returns the new "end" position.
<source lang="cpp">#include <algorithm>
- include <vector>
- include <list>
- include <iostream>
using namespace std; int main (){
list <int> l; for (int nCount = 0; nCount < 10; ++ nCount) l.push_back (nCount); list <int>::const_iterator li; for ( li = l.begin (); li != l.end (); ++ li ) cout << *li << " "; vector <int> v (l.size () * 2); vector <int>::iterator iLastPos; iLastPos = copy ( l.begin (), l.end (), v.begin () ); vector <int>::iterator i; i = remove (v.begin (), v.end (), 0); v.erase (i , v.end ()); vector <int>::iterator vi; for ( vi = v.begin (); vi != v.end (); ++ vi ) cout << *vi << " "; return 0;
}</source>
Use remove() to delete elements from a vector
<source lang="cpp">#include <iostream>
- include <vector>
- include <algorithm>
using namespace std; template<class InIter> void show_range(const char *msg, InIter start, InIter end); int main() {
vector<char> v; vector<char>::iterator itr, itr_end; for(int i=0; i<5; i++) { v.push_back("A"+i); } for(int i=0; i<5; i++) { v.push_back("A"+i); } show_range("Original contents of v:", v.begin(), v.end()); // Remove all A"s. itr_end = remove(v.begin(), v.end(), "A"); show_range("v after removing all A"s:", v.begin(), itr_end); return 0;
} template<class InIter> void show_range(const char *msg, InIter start, InIter end) {
InIter itr; cout << msg << endl; for(itr = start; itr != end; ++itr) cout << *itr << endl;
}</source>
Use std::remove to delete all element in a vector by value
<source lang="cpp">#include <iostream> using std::cout; using std::endl;
- include <algorithm>
- include <vector>
- include <iterator>
int main() {
int a[ 10 ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; std::ostream_iterator< int > output( cout, " " ); std::vector< int > v( a, a + 10 ); // copy of a std::vector< int >::iterator newLastElement; cout << "Vector v before removing all 10s:\n "; std::copy( v.begin(), v.end(), output ); // remove all 10s from v newLastElement = std::remove( v.begin(), v.end(), 10 ); cout << "\nVector v after removing all 10s:\n "; std::copy( v.begin(), newLastElement, output ); return 0;
}</source>
Vector v before removing all 10s: 10 2 10 4 16 6 14 8 12 10 Vector v after removing all 10s: 2 4 16 6 14 8 12
Use the generic remove algorithm
<source lang="cpp">#include <iostream>
- include <cassert>
- include <algorithm>
- include <vector>
using namespace std; int main() {
const int N = 11; int array1[N] = {1, 2, 0, 3, 4, 0, 5, 6, 7, 0, 8}; vector<int> vector1; for (int i = 0; i < N; ++i) vector1.push_back(array1[i]); // Remove the zeros from vector1: vector<int>::iterator new_end; new_end = remove(vector1.begin(), vector1.end(), 0); for (int i = 0; i < (int)vector1.size(); ++i) cout << vector1[i]; cout << "\n\n\n\n\n"; // The size of vector1 remains the same: assert (vector1.size() == N); cout << vector1.size(); cout << "\n\n\n\n\n"; // The nonzero elements are left in [vector1.begin(), new_end). Erase the rest: vector1.erase(new_end, vector1.end()); // Show that 3 elements were removed and the nonzero elements remain, in their original order: assert (vector1.size() == N - 3); cout << vector1.size(); cout << "\n\n\n\n\n"; for (int i = 0; i < (int)vector1.size(); ++i) cout << vector1[i]; return 0;
}</source>
12345678708 11 8 12345678