remove_copy()
它是C++中的STL函数,在算法库中定义。它将范围在[first,last)内的元素复制到从结果开始的范围内,除了那些比较等于给定元素的元素。
- 结果范围比[first,last)短,其数目与序列中匹配项“被删除”的数目相同。
- 保留未删除元素的相对顺序。
- 该函数使用运算符==将各个元素与给定值进行比较。
函数模板
ResultIterator remove_copy(ForwardIterator first, ForwardIterator last,
ResultIterator result ,const T& ele);
first, last : Forward iterators to the initial and final positions
in a sequence. The range used is [first, last), which contains all the elements
between first and last, including the element pointed by first but not
the element pointed by last.
result : Output iterator to the initial position of the range
where the resulting sequence is stored. The pointed type shall support being
assigned the value of an element in the range [first, last).
ele : element to be removed.
例子:
Input : b d a f g h a k given element is a
Output :b d f g h k _ _
Input : b k c s n m c l given element is c
Output : b k s n m l _ _
'_' represent remove places
// CPP code to demonstrate remove_copy()
#include
#include
#include
using namespace std;
// Function to remove_copy from v1 result vector is v2
void removecopyDemo(vector &v1)
{
remove_copy(v1.begin(), v1.end(), v1.begin(), 3);
}
// Function to print content of vector
void print(vector&v)
{
int len = v.size();
for (int i = 0; i < len; i++)
cout << v[i] << " ";
cout << endl;
}
// Driver code
int main()
{
// vector
vector v1, v2(10);
// push data in vector
for(int i = 10; i <= 25; i++)
v1.push_back(i % 6);
cout << "elements of v1 before remove_copy: "<
输出:
elements of v1 before remove_copy:
4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1
After removing element 3
4 5 0 1 2 4 5 0 1 2 4 5 0 1 0 1
复杂度:线性O(n)
remove_copy_if
它是C++中的STL函数,在算法库中定义。它会将范围为[first,last]的元素复制到从结果开始的范围,但那些满足给定条件的元素(例如,奇数,偶数,素数,非素数等)除外。
- 将范围为[first,last)的元素复制到从结果开始的范围,条件条件函数返回true的那些元素除外。
- 结果范围比“第一个,最后一个”短了与“删除”的匹配项相同的元素。
函数模板
ResultIterator remove_copy_if(ForwardIterator first, ForwardIterator last,
ResultIterator result, UnaryPredicate pred);
pred : Unary function that accepts an element in the range as
argument, and returns a value convertible to bool. The value returned indicates
whether the element is to be removed (if true, it is removed).
例子:
Input : 1 2 3 4 5 6 7 8 9 check if a number is prime and remove
Output :1 4 6 8 9 0 0 0 0
Input :1 2 3 4 5 6 7 8 9 check if a number is even and remove
Output :1 3 5 7 9 0 0 0 0
// CPP code to demonstrate remove_copy_if()
#include
#include
#include
using namespace std;
bool IsOdd(int i) { return ((i % 2) != 0); }
// Function to remove_copy from v1 result vector is v2
void remove_copy_ifDemo(vector &v1, vector &v2)
{
remove_copy_if(v1.begin(), v1.end(), v2.begin(), IsOdd);
}
// Function to print content of vector
void print(vector&v)
{
int len = v.size();
for (int i = 0; i < len; i++)
cout << v[i] << " ";
cout << endl;
}
// Driver code
int main()
{
// declare vector v1, v2
vector v1, v2(10);
// push data in vector
for(int i = 10; i <= 20; i++)
v1.push_back(i);
cout << "elements of v1 before remove_copy: ";
print(v1);
remove_copy_ifDemo(v1,v2);
cout << "elements of v1 after remove_copy: ";
print(v1);
cout << "After removing Odd Numbers from v1"
" copy result in vector v2" <
输出:
elements of v1 before remove_copy: 10 11 12 13 14 15 16 17 18 19 20
elements of v1 after remove_copy: 10 11 12 13 14 15 16 17 18 19 20
After removing Odd Numbers from v1 copy result in vector v2
10 12 14 16 18 20 0 0 0 0
复杂度:线性O(n)
C++ STL中的
C++的所有STL文章
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。