📄 fig20_28.cpp
字号:
// Fig. 20.28: fig20_28.cpp
// Demonstrates Standard Library functions remove, remove_if
// remove_copy and remove_copy_if
#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 };
ostream_iterator< int > output( cout, " " );
// Remove 10 from v
vector< int > v( a, a + SIZE );
vector< int >::iterator newLastElement;
cout << "Vector v before removing all 10s:\n";
copy( v.begin(), v.end(), output );
newLastElement = remove( v.begin(), v.end(), 10 );
cout << "\nVector v after removing all 10s:\n";
copy( v.begin(), newLastElement, output );
// Copy from v2 to c, removing 10s
vector< int > v2( a, a + SIZE );
vector< int > c( SIZE, 0 );
cout << "\n\nVector v2 before removing all 10s "
<< "and copying:\n";
copy( v2.begin(), v2.end(), output );
remove_copy( v2.begin(), v2.end(), c.begin(), 10 );
cout << "\nVector c after removing all 10s from v2:\n";
copy( c.begin(), c.end(), output );
// Remove elements greater than 9 from v3
vector< int > v3( a, a + SIZE );
cout << "\n\nVector v3 before removing all elements"
<< "\ngreater than 9:\n";
copy( v3.begin(), v3.end(), output );
newLastElement = remove_if( v3.begin(), v3.end(),
greater9 );
cout << "\nVector v3 after removing all elements"
<< "\ngreater than 9:\n";
copy( v3.begin(), newLastElement, output );
// Copy elements from v4 to c,
// removing elements greater than 9
vector< int > v4( a, a + SIZE );
vector< int > c2( SIZE, 0 );
cout << "\n\nVector v4 before removing all elements"
<< "\ngreater than 9 and copying:\n";
copy( v4.begin(), v4.end(), output );
remove_copy_if( v4.begin(), v4.end(),
c2.begin(), greater9 );
cout << "\nVector c2 after removing all elements"
<< "\ngreater than 9 from v4:\n";
copy( c2.begin(), c2.end(), output );
cout << endl;
return 0;
}
bool greater9( int x )
{
return x > 9;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -