1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
void removeSome( vector<T> &V, T value, int n, int i = 0 )
{
if ( n <= 0 || i >= V.size() ) return;
auto it = find( V.begin() + i, V.end(), value );
if ( it == V.end() ) return;
i = it - V.begin();
rotate( it, it + 1, V.end() );
V.pop_back();
removeSome( V, value, n - 1, i );
}
template<typename T>
ostream & operator << ( ostream &out, const vector<T> &V )
{
for ( T e : V ) out << e << ' ';
return out;
}
int main()
{
const int MaxRemove = 3, Value = 1;
vector<int> V{ 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 1, 2, 1 };
cout << "Before: " << V << '\n';
removeSome( V, Value, MaxRemove );
cout << "After: " << V << '\n';
}
| |