📅  最后修改于: 2020-10-17 04:25:11             🧑  作者: Mango
C++ STL algorithm.remove()函数用于在给定范围内(第一个,最后一个)消除所有等于val的元素,而不会干扰其余元素的顺序。
template
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
first:前向迭代器,指向要从中删除元素的范围内的第一个元素的位置。
last:一个正向迭代器,它在要删除元素的范围内将位置指向最后一个元素之后的位置。
val:要从范围中删除的值。
如果first和last相等,则前向迭代器指向修改范围或第一个元素的新结束位置(最后)。
复杂度在[first,last)范围内是线性的,并且可能会对其中的一些进行赋值。
在[first,last)范围内的对象将被访问并可能被修改。
如果任何元素比较,元素分配或迭代器上的操作均引发异常,则此函数将引发异常。
请注意,无效的参数会导致未定义的行为。
让我们看一个简单的示例来演示remove()的用法:
#include // std::cout
#include // std::remove
using namespace std;
int main () {
int myints[] = {10,20,30,50,20,40,100,20};
// bounds of range:
int* pbegin = myints;
int* pend = myints+sizeof(myints)/sizeof(int);
pend = remove (pbegin, pend, 20);
cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
cout << ' ' << *p;
cout << '\n';
return 0;
}
输出:
range contains: 10 30 50 40 100
让我们来看另一个简单的示例,以说明delete()和remove()之间的区别:
#include
#include
#include
int main()
{
using namespace std;
//Populate myvec with the data set 10, 5, -8, 5, 1, 4
vector myvec;
myvec.push_back(10);
myvec.push_back(5);
myvec.push_back(-8);
myvec.push_back(5);
myvec.push_back(1);
myvec.push_back(4);
cout << "\n Initial data set: ";
for(size_t i(0); i!=myvec.size(); ++i)
cout << myvec.at(i) << ' ';
//Remove the data elements matching '5'
vector::iterator invalid;
invalid = remove( myvec.begin(), myvec.end(), 5 );
cout << "\n\n Data set after remove: ";
for(size_t i(0); i!=myvec.size(); ++i)
cout << myvec.at(i) << ' ';
//Destroy the remaining invalid elements
myvec.erase( invalid, myvec.end() );
cout << "\n\n Data set after erase: ";
for(size_t i(0); i!=myvec.size(); ++i)
cout << myvec.at(i) << ' ';
return 0;
}
输出:
Initial data set: 10 5 -8 5 1 4
Data set after remove: 10 -8 1 4 1 4
Data set after erase: 10 -8 1 4
让我们看另一个简单的例子:
#include
#include
#include
using namespace std;
bool IsOdd(int i)
{
return ((i % 2) == 1);
}
// Driver code
int main ()
{
vector vec1 { 10, 20, 30, 30, 20, 10, 10, 20};
// Print original vector
cout << "Original vector : ";
for(int i=0; i < vec1.size(); i++)
cout << " " << vec1[i];
cout << "\n";
// Iterator that store the position of last element
vector ::iterator pend;
// std ::remove function call
pend = remove (vec1.begin(), vec1.end() , 20);
// Print the vector
cout << "After remove : ";
for ( vector :: iterator p=vec1.begin(); p != pend; ++p)
cout << ' ' << *p;
cout << '\n';
return 0;
}
输出:
Original vector : 10 20 30 30 20 10 10 20
After remove : 10 30 30 10 10
让我们看另一个简单的例子:
#include
#include
#include
int main( ) {
using namespace std;
vector v1;
vector ::iterator Iter1, Iter2, new_end;
int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );
random_shuffle ( v1.begin( ), v1.end( ) );
cout << "Vector v1 is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Remove elements with a value of 7
new_end = remove ( v1.begin( ), v1.end( ), 7 );
cout << "Vector v1 with value 7 removed is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// To change the sequence size, use erase
v1.erase (new_end, v1.end( ) );
cout << "Vector v1 resized with value 7 removed is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
return 0;
}
输出:
Vector v1 is ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
Vector v1 with value 7 removed is ( 4 0 5 1 6 9 3 8 2 9 3 7 8 2 ).
Vector v1 resized with value 7 removed is ( 4 0 5 1 6 9 3 8 2 ).