Thread: STL map with D3DXVECTOR2 as key help.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    STL map with D3DXVECTOR2 as key help.

    Hi everyone, I am still a relatively new programmer. I have used STL to some extent previously but my usage to maps has always been with using built-in types as the key.

    Currently, I am tasked with doing something that I need to keep track of unique 2D data on a UV map. The generated coordinates may iterate over a 2D coordinate a few times but I only want to keep track of a single set of 2D data (therefore my choice of using map is correct?)

    Anyway, here's some code for the experts here to digest.

    In the header file...
    Code:
    typedef struct VECTOR2 : public D3DXVECTOR2
    {
    public:
    	VECTOR2( float x, float y );
    	bool operator < ( CONST VECTOR2& ) const;
    	} VECTOR2;
    struct wave_compare : public std::binary_function<VECTOR2, VECTOR2, bool>
    {
    	bool operator() (const VECTOR2& left, const VECTOR2& right) const
    	{	
    		if(	left.x < right.x || left.y < right.y )
    			return true;
    		else
    			return false;
    	}
    };
    
    typedef std::pair<VECTOR2,VECTOR2>							wavePointPair;
    typedef std::map<VECTOR2,VECTOR2,wave_compare>				wavePointMap;
    typedef std::map<VECTOR2,VECTOR2,wave_compare>::iterator	wavePointMapIter;
    wavePointMap					waveInfo;
    In the CPP file...
    Code:
    bool 
    CrsEffectTest :: VECTOR2 :: operator<( CONST VECTOR2 &right) const
    {
    	if(	x < right.x || y < right.y )
    		return true;
    	else
    		return false;
    }
    
    CrsEffectTest :: VECTOR2 :: VECTOR2( float xx, float yy )
    {
    	x = xx;
    	y = yy;
    }
    Insertion into the map here like this...
    Code:
    waveInfo.insert( wavePointPair( VECTOR2( vtx.x, vtx.y ), VECTOR2( vtx.u, vtx.v ) ) );
    I am getting errors at runtime
    Code:
    Assertion failed: invalid operator<
    I presume that there is no 'correct' way to sort a 2D data but for me, I just want them to be easily reference with the find method to adjust the elements...

    BTW, there is no error when && is used in the comparison above but that screws up the comparison and many 2D coordinates cannot be inserted because they are seen as non-unique?

    Help! Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If left.x is less than right.x, return true. If less.x is greater than right.x return false. If they are equal, then check y. If left.y is less than right.y return true. Otherwise return false.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM