Comparing two vectors of arbitrary size, block by block I am trying to compare two vectors which are of size of multiple of 4. Each vector block has a unique number it e.g {0,0,0,0}, {0,0,0,1}, {0,0,0,2} or {0,0,0,0,0,0,0,1,0,0,0,2} and {0,0,0,2,0,0,0,1} so on. I first divide the vector in block of 4 and than compare it and save the block which is not present in another vector into a global vector. I wrote a small function which do the job but often shows segmentation error.

void comparing_vectors_by_block(std::vector&lt;int&gt; vector_A, std::vector&lt;int&gt; 
vector_B)
{
int blockSize = 4;
std::vector&lt;int&gt;::iterator start_of_A = vector_A.begin();
std::vector&lt;int&gt;::iterator start_of_B = vector_B.begin();

std::vector&lt;int&gt; subList_A;
std::vector&lt;int&gt; subList_B;
std::cout &lt;&lt; "size of the vector_A: " &lt;&lt; vector_A.size() &lt;&lt; std::endl;
std::cout &lt;&lt; "size of the vector_B: " &lt;&lt; vector_B.size() &lt;&lt; std::endl;
for (auto i = 0; i &lt; vector_A.size() / blockSize; i++)
{
    subList_A.assign(start_of_A, start_of_A + blockSize);
    for (auto i = 0; i &lt; vector_B.size() / blockSize; i++)
    {
        subList_B.assign(start_of_B, start_of_B + blockSize);
        if (subList_A != subList_B)
        {

            hold_result.insert(hold_result.begin(), start_of_B, start_of_B + blockSize);
            start_of_B += blockSize;
        }
        else
        {
            start_of_B += blockSize;
            std::cout &lt;&lt; "both vectors are not equal: " &lt;&lt; std::endl;
        }
    }
    start_of_A += blockSize;
}
If at all possible, I'd start from a slightly different direction. I'd start by defining the group of 4 ints as a type:

static const int blocksize = 4;

class block {
    std::array&lt;int, blocksize&gt; data;

    bool operator&lt;(block const &amp;other) { 
        for (int i=0; i&lt;blocksize; i++)
            if (data[i] &lt; other.data[i])
                return true;
            if (other.data[i] &lt; data[i])
                return false;
        }
        return false;
    }
};


Having done that, you can use a standard algorithm to get the desired result:

std::vector&lt;block&gt; find_diff(std::vector&lt;block&gt; a, std::vector&lt;block&gt; b) {
    std::vector&lt;block&gt; difference; // where we'll put the result

    // set operations require sorted inputs   
    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    std::set_difference(a.begin(), a.end(), 
                        b.begin(), b.end(), 
                        std::back_inserter(difference));
    return difference;
}