vendredi 29 mai 2015

Finding vectors in C++

I have two vectors of vector<unsigned> namely: vector<vector<unsigned> > sbp, vector<vector<unsigned> > sp. I want to print all those vectors in sbp which are also in sp. Both vectors sbp and sp are stored (i) first by size; (ii) and when the size are equal, then the vectors are sorted lexicographically. I wrote the following code for doing the same. The code appears to be giving segmentation fault. I debugged the code (by printing out values), but I am not able to find the source of the error.

Can someone please help me find what could be the source of segmentation fault. Also if there is some algorithm which is faster than this, then that will be really great

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    vector<vector<unsigned> > sbp;
    vector<vector<unsigned> > sp;
    vector<vector<unsigned> >::iterator itSP=sp.begin();   
    for(vector<vector<unsigned> >::iterator itSbp=sbp.begin(),lSbp=sbp.end();itSbp!=lSbp;)
    {
        if(std::lexicographical_compare(((*itSbp)).begin(), ((*itSbp)).end(), (*itSP).begin(), (*itSP).end()))
        {
            itSbp++;
        }else{
            if((*itSbp)==(*itSP)) 
            {
                cout<<(*itSbp)<<"\n";
                itSbp++;
            }else{
                itSP++;                
            }            
        }
    }
}

I am using C++11(gcc 4.8)

Aucun commentaire:

Enregistrer un commentaire