What basically I开发者_JAVA百科 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.
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());
}
You shouldn't need to define a vector sorting predicate. Vector already defines an
operator<
which is based onstd::lexicographical_compare
.Is this what you mean?
std::for_each(rows.begin(), rows.end(), [](std::vector<int>& row) {
row.erase(std::remove_if(row.begin(), row.end(), [](int number) -> bool {
return (number == 2) || (number == 3);
}), row.end());
});
精彩评论