[Insight-users] Defining a map of IndexTypes

Luis Ibanez luis.ibanez at kitware.com
Sun Nov 1 15:53:43 EST 2009


Hi Matthias,

As Karthik pointed out, your initial attempt may
still be a correct way of declaring a std::map.


Please find below the modified code that compiles fine:


#include "itkImage.h"

template <class TImageType>
class TEST
{
public:

typedef typename    TImageType::IndexType       IndexType;

typedef bool (*comp_t)( IndexType const &, IndexType const & );

static bool fless(const IndexType& lhs, const IndexType& rhs)
  {
  //y1 is lower y2
  if(lhs[1]<rhs[1])return true;
  //y1==y2 and x1 < x2
  if((lhs[1]==rhs[1])&&(lhs[0]<rhs[0]))return true;
  //else
  return false;
  }

typedef std::map<IndexType, bool, comp_t>  MapType;

void Test()
  {
  MapType recursionPoints( fless );
  }

};

int main()
{
typedef itk::Image< char, 3 >  ImageType;
typedef TEST<ImageType>        TestType;

TestType tt;

tt.Test();

return 0;
}

---


Regards,


         Luis


------------------------------------------------------------------
On Wed, Oct 28, 2009 at 7:12 AM, Matthias Dodt
<matthias.dodt at mdc-berlin.de> wrote:
> Sorry, but cant get it to work with the additional struct in the class...
> but i think it shoud be something like:
>
> std::map<IndexType, bool, TEST<myImageType>::ltstr > recursionPoints;
>
> instead of:
>
> std::map<IndexType, bool, ltstr> recursionPoints;
>
>
> I removed the struct from the class, then it seems to work:
>
> std::map<IndexType, bool, TEST<myImageType> > recursionPoints;
>
> I already read about the function pointers but was wondering how to use them
> with templates-
>
> Thanks!
>
> Greetings
>
> mat
>
>
> Luis Ibanez schrieb:
>>
>> Hi Matthias,
>>
>> You are mis-constructing the map type.
>>
>> The code that you need is like the following:
>>
>> --------------------------------------------------------------------------
>> #include "itkImage.h"
>>
>> template <class TImageType>
>> class TEST
>> {
>> typedef typename    TImageType::IndexType       IndexType;
>>
>> struct ltstr
>> {
>>  bool operator()(const IndexType& lhs, const IndexType& rhs) const
>>    {
>>    //y1 is lower y2
>>    if(lhs[1]<rhs[1])return true;
>>    //y1==y2 and x1 < x2
>>    if((lhs[1]==rhs[1])&&(lhs[0]<rhs[0]))return true;
>>    //else
>>    return false;
>>    }
>> };
>>
>>
>> std::map<IndexType, bool, ltstr> recursionPoints;
>>
>> };
>>
>> int main()
>> {
>> return 0;
>> }
>>
>>
>>
>> Note that the comparator, the third template argument
>> of the std::map is actually a class, that has a method
>> operator().
>>
>> More at:
>> http://www.sgi.com/tech/stl/Map.html
>>
>>
>>      Regards,
>>
>>
>>             Luis
>>
>>
>> ---------------------------------
>> On Tue, Oct 27, 2009 at 12:00 PM, Matthias Dodt
>> <matthias.dodt at mdc-berlin.de> wrote:
>>
>>>
>>> Hi there!
>>>
>>> I want to store IndexTypes in a map (as keys). I got the following code:
>>>         ...
>>>
>>>          typedef typename    TImageType::IndexType       IndexType;
>>>
>>>          bool fless(const IndexType& lhs, const IndexType& rhs) const
>>>          {
>>>            //y1 is lower y2
>>>            if(lhs[1]<rhs[1])return true;
>>>            //y1==y2 and x1 < x2
>>>            if((lhs[1]==rhs[1])&&(lhs[0]<rhs[0]))return true;
>>>            //else
>>>            return false;
>>>          }
>>>
>>>          typedef bool (*comp_t)( IndexType const &, IndexType const & );
>>>
>>>          std::map<IndexType, bool, comp_t> recursionPoints(fless);
>>>
>>> ...
>>>
>>> Anyway- i get an error saying that fless has no type. I think the problem
>>> is
>>> that IndexType is a template type...
>>>
>>> thanks!
>>>
>>>
>>> _____________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Kitware offers ITK Training Courses, for more information visit:
>>> http://www.kitware.com/products/protraining.html
>>>
>>> Please keep messages on-topic and check the ITK FAQ at:
>>> http://www.itk.org/Wiki/ITK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.itk.org/mailman/listinfo/insight-users
>>>
>>>
>


More information about the Insight-users mailing list