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 47 48 49 50 51 52 53
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool condition( int a, int b ){
return a+1 == b;
}
int arr[] = {19, 1, 2, 3};
//int arr[] = {19, 19, 19};
//int arr[] = {3, 4, 19, 1, 2};
//int arr[] = {1, 2, 3, 19, 19};
int main() {
vector<int> v(sizeof(arr)/sizeof(int));
vector<int> nv; // The other vector to which the range will be copied
copy( arr, arr+sizeof(arr)/sizeof(int), v.begin() );
int first=-1, last=-1;
bool prev = condition( v.back(), v.front() ), current;
for( unsigned i = 0; i < v.size()+1; i++ ) {
int a = i >= v.size() ? i-v.size() : i;
int b = i+1 >= v.size() ? i+1-v.size() : i+1;
if( current = condition( v[a], v[b] ) ){
if( prev == false ) first = a;
}else if( prev == true ) last = b;
prev = current;
}
if (last==0 && !condition(v.back(),v.front())) last=v.size(); // Added this line
if( first < last ) {
nv.insert(nv.begin(), v.begin()+first, v.begin()+last);
v.insert( v.erase( v.begin()+first+1, v.begin()+last-1 ), 2520 );
}else if(first > last){
nv.insert(nv.begin(),v.begin()+first,v.end());
nv.insert(nv.end(),v.begin(),v.begin()+last);
v.erase( v.begin()+first+1, v.end() );
v.erase( v.begin(), v.begin()+last-1 );
v.insert( v.end(), 2520 );
}
for( vector<int>::iterator it = v.begin(); it != v.end(); it++ ) cout << *it << ' ';
cout << '\n';
for( vector<int>::iterator it = nv.begin(); it!= nv.end(); it++) cout << *it << ' ';
cin.get();
return 0;
}
|