vendredi 24 mars 2017

Specify which types should compare when using unique with vector of pairs

I am trying to remove duplicates from my vector using unique, but I can't remove duplicates because one of my data types doesn't have compare operator.

This works :

 std::vector<std::pair<int,int>> my_vec;
 std::sort(my_vec.begin(), my_vec.end());
 auto it = std::unique(my_vec.begin(), my_vec.end());

 my_vec.erase(it, my_vec.end());

But, I am currently using sf::Text object from SFML lib, and It doesn't have compare operator.

 std::vector<std:pair<sf::Text, bool>> my_vec;
 // THE REST OF CODE IS SAME AS CODE ABOVE

Now if I use auto it = std::unique(my_vec.begin(), my_vec.end()); it throws compile time error which is something like this :

X:\my_dir\SnakeGame\packages\VisualCppTools.14.0.25114-Pre\lib\native\include\utility(290): note: while trying to match the argument list '(const sf::Text, const sf::Text)'

X:\my_dir\SnakeGame\packages\VisualCppTools.14.0.25114-Pre\lib\native\include\xstddef(302): note: see reference to function template instantiation 'bool std::operator ==(const std::pair &,const std::pair &)' being compiled

X:\my_dir\SnakeGame\packages\VisualCppTools.14.0.25114-Pre\lib\native\include\algorithm(1469): note: see reference to function template instantiation 'bool std::equal_to::operator ()&,std::pair&>(_Ty1,_Ty2) const' being compiled

with [ _Ty1=std::pair &, _Ty2=std::pair & ]

X:\my_dir\SnakeGame\packages\VisualCppTools.14.0.25114-Pre\lib\native\include\algorithm(1486): note: see reference to function template instantiation '_FwdIt std::_Unique_unchecked*,_Pr>(_FwdIt,_FwdIt,_Pr &)' being compiled

with [ _FwdIt=std::pair *, _Pr=std::equal_to ]

X:\my_dir\SnakeGame\packages\VisualCppTools.14.0.25114-Pre\lib\native\include\algorithm(1493): note: see reference to function template instantiation '_FwdIt std::unique<_FwdIt,std::equal_to>(_FwdIt,_FwdIt,_Pr)' being compiled

with [ _FwdIt=std::_Vector_iterator>>>, _Pr=std::equal_to ]

main.cpp(634): note: see reference to function template instantiation '_FwdIt std::unique>>>>(_FwdIt,_FwdIt)' being compiled

with [ _FwdIt=std::_Vector_iterator>>> ]

If it is not possible to do, maybe I could create one more pair inside pair which would contain some other data like strings, ints, which I could use for comparison, but then how could I only check for "my_vec[i].first" elements and not second element ?

Aucun commentaire:

Enregistrer un commentaire