The most efficient way to remove an element from a vector is the back-swap trick, but this only applies if you don't care about order.

#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
    std::vector&lt;int&gt; v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    auto it = v.begin() + 5;
    // replace the current element with the back of the vector,
    // then shrink the size of the vector by 1.
    *it = std::move(v.back());
    v.pop_back();

    for (auto n : v) {
        std::cout &lt;&lt; n &lt;&lt; " ";
    }
    std::cout &lt;&lt; "\n";
}


http://ideone.com/0jbWHZ

If you know there are going to be many deletes or a very large vector, you may retain efficiency by using this trick, remembering not to ++ your current iterator after doing a delete, and std::sort()ing the vector when you reach the end.

--- Edit ---

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

//! Efficiently remove an element from a vector without
//! preserving order. If the element is not the last element
//! in the vector, transfer the last element into its position
//! using a move if possible.
//! Regardless, we then shrink the size of the vector deleting
//! the element at the end, which will either be destructed or
//! the element we were deleting.
//! @note: Effectively invalidates the current iterator.
template&lt;class ValueType&gt;
bool unstable_remove(
    typename std::vector&lt;ValueType&gt;&amp; container,
    typename std::vector&lt;ValueType&gt;::iterator it
    )
{
    // Leave in-situ if we are already the tail element.
    auto lastEl = container.end() - 1;
    if (it != lastEl) {
        // overwrite this element with what is in the last,
        // which should have the same effect as deleting this.
        *it = std::move(*lastEl);
    }
    // release the last cell of the vector, because it should
    // now either be destructed or contain the value we were
    // deleting.
    container.pop_back();
}

int main()
{
    std::vector&lt;int&gt; ints { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    auto it = ints.begin();
    while (it != ints.end()) {
        if ((*it % 3) == 0) {
            unstable_remove(ints, it);
            // do not pass go / ++it
            continue;
        }
        ++it;
    }
    std::cout &lt;&lt; "after removes:\n";
    for (auto val : ints)
        std::cout &lt;&lt; val &lt;&lt; " ";
    std::cout &lt;&lt; "\n";
    std::sort(ints.begin(), ints.end());
    std::cout &lt;&lt; "after sort:\n";
    for (auto val : ints)
        std::cout &lt;&lt; val &lt;&lt; " ";
    std::cout &lt;&lt; "\n";
}


Produces (http://ideone.com/hGZPOC)

after removes:
1 2 10 4 5 8 
after sort:
1 2 4 5 8 10 


--- Edit 2 ---

Here's a cleanup of your code for readability, I also ditched your end captures because ... you are deleting elements.

#include &lt;vector&gt;
#include &lt;cstdint&gt;

using vec_t = std::vector&lt;uint32_t&gt;;
using vecpair_t = std::pair&lt;vec_t, vec_t&gt;;
using pairvec_t = std::vector&lt;vecpair_t&gt;;

int main(int argc, char** argv) {
    pairvec_t pairV1V2;
    for(auto itm = pairV1V2.begin(); itm != pairV1V2.end(); ++itm)
    {
        //Outer values
        auto&amp; outerV1 = itm-&gt;first; // NOTE '&amp;' - reference not copy!
        auto&amp; outerV2 = itm-&gt;second;
        sort(outerV2.begin(), outerV2.end());
        for(auto itm2 = itm + 1; itm2 != pairV1V2.end(); ++itm2)
        {
            auto&amp; innerV1 = itm2-&gt;first;
            auto&amp; innerV2 = itm2-&gt;second;
            vec_t setDiffV1;


As for another way to optimize this - since your lists are sorted - walk both lists at the same time comparing values.

template&lt;typename ValueType&gt;
void dedupe_vectors(
    typename std::vector&lt;ValueType&gt;&amp; lhs,
    typename std::vector&lt;ValueType&gt;&amp; rhs
    )
{
    auto lit = lhs.begin();
    auto rit = rhs.begin();
    while (rit != rhs.end) {
        while (lit != lhs.end() &amp;&amp; *lit &lt; *rit)
            ++lit;
        if (lit == lhs.end())
            break;
        if (*lit == *rit) {
            v2.erase(rit);
            continue;
        }  
        ++rit;
    }
}


I know - we test lit vs lhs.end twice. Take a look at the code your compiler generates with -O3 and see if it doesn't detect this itself. If so, then you can worry about optimizing it.

How to efficiently delete elements from vector c++
Could you possibly help me on this question please?

"All right, Johhny, it's time  you ..........to bed."


went
would go
will be going
going to go


I would have gone for the word "go" but it is not in options.

Another similar question is that

" Your hair is too long.It is time .............a haircut" 

Thanks
