Select right elements inside a vector

Thursday, July 3, 2014

I explane as clear as possible my problem: I have two vectors prova1 and prova2. Inside prova1 there is a collection of int numbers i.e.



prova1={0;1;2;4;6;7,8;9;10;11;12;13};


prova2 is a subset of prova1 i.e.



prova2={2;4;6;8;10;12;13};


my goal is to copy in a third vector (called "prova") only the sequences of elements inside prova2 that are inside prova1 and contigous. For the vectors discribed above respect to the conditions I have two sequences: {2;4;6} and {12;13}, in fact this two sequences have no separation elements inside prova1.


Here my code to approach this problem:



vector<int>prova1;
vector<int>prova2;
vector<int>prova;
prova1.push_back(0);prova1.push_back(1);prova1.push_back(2);prova1.push_back(4);prova1.push_back(6);prova1.push_back(7);
prova1.push_back(8);prova1.push_back(9);prova1.push_back(10);prova1.push_back(11);prova1.push_back(12);prova1.push_back(13);
prova2.push_back(2);prova2.push_back(4);prova2.push_back(6);prova2.push_back(8);prova2.push_back(10);prova2.push_back(12);prova2.push_back(13);

for(int i=1;i<=prova1.size()-1;i++){
for(int j=1;j<=prova2.size()-1;j++){
if(prova2[j]==prova1[i]){
int variabile=prova2[j-1];
if(prova1[i-1]==variabile){
prova.push_back(prova2[j]);

}
}

}

}


but as you can see there is an issue:



  1. I can't include the first element of any sequences


please try give my some help to solve it.







http://ift.tt/1mb0NKf