Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:25:09+00:00 2026-05-22T22:25:09+00:00

What basically I am doing is the set cover problem, removing duplicate from the

  • 0

What basically I am doing is the set cover problem, removing duplicate from the vectors that has the same numbers. An example:

I have the following vectors of vectors after sorting:

{{1,2,3,4,5},{2,3,7,8},{10,11,12}}

NOW, I would like to remove the occurrence from 2nd vector which is 2,3 and sort again …

 {{1,2,3,4,5},{10,11,12},{7,8}}

I have implemented some code to sort the vectors of vectors but i have problem removing the occurrences from the vector that has less size?

The sort function:

sort(ROWS.begin(),ROWS.end(),VectorsSort());

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:25:10+00:00Added an answer on May 22, 2026 at 10:25 pm

    Pick this apart and take what you need:

    #include <algorithm>
    #include <vector>
    
    struct binary_search_pred
    {
        typedef bool result_type;
    
        explicit binary_search_pred(std::vector<int> const& v) : v_(&v) { }
    
        bool operator ()(int const& i) const
        {
            return std::binary_search(v_->begin(), v_->end(), i);
        }
    
    private:
        std::vector<int> const* v_;
    };
    
    struct size_comparison_desc
    {
        typedef bool result_type;
    
        typedef std::vector<int> const& arg_t;
        bool operator ()(arg_t a, arg_t b) const
        {
            return b.size() < a.size();
        }
    };
    
    void set_cover(std::vector<std::vector<int> >& vec)
    {
        typedef std::vector<std::vector<int> > vec_t;
        typedef vec_t::iterator iter_t;
        typedef vec_t::const_iterator citer_t;
    
        if (vec.empty() || vec.size() == 1)
            return;
    
        for (iter_t v = vec.begin() + 1, v_end = vec.end(); v != v_end; ++v)
            for (citer_t p = vec.begin(); p != v; ++p)
                v->erase(
                    std::remove_if(v->begin(), v->end(), binary_search_pred(*p)),
                    v->end()
                );
        std::sort(vec.begin(), vec.end(), size_comparison_desc());
    }
    

    (Note that set_cover requires that the contained std::vector<int>s given must already be sorted.)


    EDIT #1:

    As requested in now-deleted comments, a version oriented around std::map<int, std::vector<int>> instead of std::vector<std::vector<int>> (use binary_search_pred from the original code):

    #include <algorithm>
    #include <map>
    #include <vector>
    
    void set_cover(std::map<int, std::vector<int> >& m)
    {
        typedef std::map<int, std::vector<int> > map_t;
        typedef map_t::iterator iter_t;
        typedef map_t::const_iterator citer_t;
    
        if (m.empty() || m.size() == 1)
            return;
    
        for (iter_t v = ++m.begin(), v_end = m.end(); v != v_end; ++v)
            for (citer_t p = m.begin(); p != v; ++p)
                v->second.erase(
                    std::remove_if(
                        v->second.begin(),
                        v->second.end(),
                        binary_search_pred(p->second)
                    ),
                    v->second.end()
                );
    }
    

    Note that the map here will always be sorted by its key, never by the contained vector‘s size (which is what you appear to want). Maybe you want a std::vector<std::pair<int, std::vector<int>>> instead?


    EDIT #2:

    As requested in now-deleted comments, a version oriented around std::vector<std::pair<int, std::vector<int>>> instead of std::map<int, std::vector<int>> (use binary_search_pred from the original code):

    #include <algorithm>
    #include <utility>
    #include <vector>
    
    struct size_comparison_desc
    {
        typedef bool result_type;
    
        typedef std::pair<int, std::vector<int> > const& arg_t;
        bool operator ()(arg_t a, arg_t b) const
        {
            return b.second.size() < a.second.size();
        }
    };
    
    void set_cover(std::vector<std::pair<int, std::vector<int> > >& vec)
    {
        typedef std::vector<std::pair<int, std::vector<int> > > vec_t;
        typedef vec_t::iterator iter_t;
        typedef vec_t::const_iterator citer_t;
    
        if (vec.empty() || vec.size() == 1)
            return;
    
        for (iter_t v = vec.begin() + 1, v_end = vec.end(); v != v_end; ++v)
            for (citer_t p = vec.begin(); p != v; ++p)
                v->second.erase(
                    std::remove_if(
                        v->second.begin(),
                        v->second.end(),
                        binary_search_pred(p->second)
                    ),
                    v->second.end()
                );
        std::sort(vec.begin(), vec.end(), size_comparison_desc());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a FooControl (I did not set the Height/Width explicitly) added to
I'm doing a some research on the feasiblity of a Sharepoint project that has
I'd like to write a function that will create and return a set of
Basically i have a form with around 100 elements. The values bind to my
Basically, I am trying to use data from one model to trigger a switch
I'm working on a functional test that needs to assert that a certain XHTML
I have a series of tests and cases in a database. Whenever a test
I'm doing a INNER JOIN across 6 tables for a module for my application,
I've been tracking down a few memory leaks in my application. It's been a
If someone could just talk to me and maybe point me in a direction.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.