Remove duplicates of vector problem

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
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
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
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.
Thank you.

Finally code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    std::vector<channel> channels;
    template <typename T>
    void remove_duplicates(vector<T>& vec)
    {

        auto same_i = [](const T& v1, const T& v2)
        {
            return v1.url == v2.url;
        };
        std::sort(vec.begin(), vec.end(),[] ( const T& lhs, const T& rhs )
        {
            return lhs.url<rhs.url;
        });
        vec.erase(std::unique(vec.begin(), vec.end(),same_i), vec.end());
    }
Topic archived. No new replies allowed.