May 28, 2021 at 12:11pm UTC
Hello. I have the classic remove duplicates of vector.
At line remove_duplicates(channels); compiler say channel has no member url.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
struct channel
{
wxString url;
wxString dir;
};
std::vector<channel> channels;
template <typename T>
void remove_duplicates(vector<T>& vec)
{
auto comp = [] ( const vector<T>& lhs, const vector<T>& rhs )
{
if (lhs.url.IsSameAs(rhs.url)) return true ;
return false ;
};
std::sort(vec.begin(), vec.end());
vec.erase(std::unique(vec.begin(), vec.end(),comp), vec.end());
}
template <typename T>
void remove_wx_duplicates(wxVector<T>& vec)
{
wxSort(vec.begin(), vec.end());
vec.wxErase(wxUnique(vec.begin(), vec.end()), vec.end());
}
I don't know what is wrong.
Thank you
Jim
Last edited on May 28, 2021 at 12:16pm UTC
May 28, 2021 at 12:51pm UTC
Please paste the actual error message.
I suspect the error message actually said that vector<channel> has no member url.
Look closely at line 14. I think you want const T& in line 13, not const vector<T>&.
And you never tell sort what lambda to use.
Last edited on May 28, 2021 at 12:52pm UTC
May 28, 2021 at 3:09pm UTC
You have right. I just change it to const T& in line 13.
But now compiler say a function-definition is not allowed before { token.
1 2 3 4 5 6 7 8 9 10 11 12 13
std::vector<channel> channels;
template <typename T>
void remove_duplicates(vector<T>& vec)
{
bool comp(const T& lhs, const T& rhs)
{
if (lhs.url.IsSameAs(rhs.url)) return true ;
return false ;
};
std::sort(vec.begin(), vec.end());
vec.erase(std::unique(vec.begin(), vec.end(),comp), vec.end());
}
And you never tell sort what lambda to use.
Here i have no integers, but struct. How to compare struct of strings?
Just changing to code bellow
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::vector<channel> channels;
template <typename T>
void remove_duplicates(vector<T>& vec)
{
auto comp = [] ( const T& lhs, const T& rhs )
{
if (lhs.url.IsSameAs(rhs.url)) return true ;
return false ;
};
std::sort(vec.begin(), vec.end());
vec.erase(std::unique(vec.begin(), vec.end(),comp), vec.end());
}
compiler say... no match for operator < ....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
namespace __gnu_cxx
{
namespace __ops
{
struct _Iter_less_iter
{
template <typename _Iterator1, typename _Iterator2>
_GLIBCXX14_CONSTEXPR
bool
operator ()(_Iterator1 __it1, _Iterator2 __it2) const
{ return *__it1 < *__it2; }
};
_GLIBCXX14_CONSTEXPR
inline _Iter_less_iter
__iter_less_iter()
{ return _Iter_less_iter(); }
I don’t know how to write to sort vector<channel>.
I am stack.
Thank you
Jim.
Last edited on May 28, 2021 at 3:17pm UTC
May 28, 2021 at 3:30pm UTC
std::string can be compared (lex) already with just a < ... you can wrap that in a lambda and put it into std::sort to use.
or you can just write a comparison operator for your struct, by operator overloading.