I am trying to resort a STL set by creating a new set and creating a custom sort function with a custom sort function , please refer to my previous qn on how its done
This is a dumbed down version of the code sample which works in Quincy 2005(G++ compiler) but does not work in Microsoft studios 2010
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <cmath>
using namespace std;
class Point2D
{
public:
int getX() const;
int getY() const;
void setX(int);
void setY(int);
bool operator < ( const Point2D& x2) const
{
if ( x != x2.x)
{
return x < x2.x;
}
if ( y != x2.y)
{
return y < x2.y;
}
};
protected:
int x;
int y;
};
struct SortByYX
{
bool operator ()(const Point2D& ptd1, const Point2D& ptd2) const
{
if ( ptd1.getY() != ptd2.getY())
{
return ptd1.getY() < ptd2.getY();
}
if ( ptd1.getX() != ptd2.getX() )
{
return ptd1.getX() < ptd2.getX();
}
return false;
}
};
int main()
{
set<Point2D> p2d_set;
Point2D p2d;
p2d.setX(1);
p2d.setY(3);
p2d_set.insert(p2d);
p2d.setX(3);
p2d.setY(2);
p2d_set.insert(p2d);
set<Point2D>::iterator p2 = p2d_set.begin();
while ( p2 != p2d_set.end() )
{
cout<<p2->getX()
<<" "
<<p2->getY()
<<endl;
p2++;
}
set<Point2D, SortByYX> p2d_set2(p2d_set.begin(), p2d_set.end());
set<Point2D>::iterator p22 = p2d_set2.begin();
cout<<endl
<<endl;
while ( p22 != p2d_set2.end() )
{
cout<<p22->getX()
<<" "
<<p22->getY()
<<endl;
p22++;
}
}
int Point2D::getX() const
{
return x;
}
int Point2D::getY() const
{
return y;
}
void Point2D::setX(int x1)
{
x = x1;
}
void Point2D::setY(int y1)
{
y = y1; ;
}
I am getting the following errors when it is compiled in MS Studio 2010 , it compiles fine in Quincy 2005
error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'std::_Tree_const_iterator<_Mytree>'
Error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_Tree_const_iterator<_Mytree>' (or there is no acceptable conversion
system_error(425): could be 'bool std::operator !=(const std::error_code &,const std::error_condition &)'
I am sure it has something to do with the custom sort function as the problem arises when i attempt to create a new set using SortByYX as my comparison function
struct SortByYX
{
bool operator ()(const Point2D& ptd1, const Point2D& ptd2) const
{
if ( ptd1.getY() != ptd2.getY())
{
return ptd1.getY() < ptd2.getY();
}
if ( ptd1.getX() != ptd2.getX() )
{
return ptd1.getX() < ptd2.getX();
}
return false;
}
};
I need help
1) understanding why does this sample of code work in Qunicy2005 but not in MS Studio 2010
2) How do i get the sample of code working in MS Studio 2010 also