copy() algorithm是很好用的algorithm,但偏偏就是沒有copy_if(),但透過remove_copy_if()則可達到相同的要求。
remove_copy_if()的思考方式和copy_if()相反,若UnaryPredicate為true,則不copy,若為false,則copy。
此範例demo若為remove_copy_if() algorithm,先輸出奇數,再輸出偶數。
1
/**/
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
Filename : GenericAlgo_remove_copy_if.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to use remove_copy_if() algorithm
7
Release : 11/12/2006 1.0
8
*/
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
#include
<
iostream
>
11
#include
<
algorithm
>
12
#include
<
vector
>
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
using
namespace
std;
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
bool
isOdd(
int
);
17
bool
isEven(
int
);
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
20
vector<int> ivec;
21
copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(ivec));
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
remove_copy_if(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, " "), isEven);
24
cout << endl;
25
remove_copy_if(ivec.begin(), ivec.end(), ostream_iterator<int>(cout," "), isOdd);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
return 0;
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
bool
isOdd(
int
val)
{
31
return val%2;
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
bool
isEven(
int
val)
{
35
return !(val%2);
36
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
執行結果
1
1
2
3
4
5
6
2
^Z
3
1
3
5
4
2
4
6
請按任意鍵繼續 . . .
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)