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 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <typename Container, typename T> void moveToEnd( Container &C, int size, T value )
{
int to = 0, from = 1;
for ( ; to < size; to++ )
{
if ( C[to] == value ) // Value to be moved to end
{
if ( from <= to ) from = to + 1;
while ( from < size && C[from] == value ) from++; // Find next non-value
if ( from >= size ) return;
C[to] = C[from];
C[from] = value;
}
}
}
int main()
{
int A[] = { 3, 4, 0, 0, 2, 0, 6, 9 };
int Aval = 0;
vector<int> B = { 13, 14, 0, 0, 20, 0, 16, 19 };
int Bval = 0;
string C = "CDxxBxFI";
char Cval = 'x';
cout << "\nInteger array before: "; for ( int i : A ) cout << i << ' ';
moveToEnd( A, sizeof( A ) / sizeof( A[0] ), Aval );
cout << "\nInteger array after: "; for ( int i : A ) cout << i << ' ';
cout << "\nVector before: "; for ( int i : B ) cout << i << ' ';
moveToEnd( B, B.size(), Bval );
cout << "\nVector after: "; for ( int i : B ) cout << i << ' ';
cout << "\nString before: "; cout << C;
moveToEnd( C, C.size(), Cval );
cout << "\nString after: "; cout << C;
}
|