954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

comparing a number to an undefined number of values

Hi I have a problem, I hope you can help me with.

I have a number, which I need to compare to some values.

I do not wish to go through all the values, and compare them individually, as I there are too many for it to be efficient.

Neither do I want to create an array with the size of the range of numbers, as it can anything from 0 UINT_MAX

So I am hoping that you might have a better solution.

Here is an example of what I have tried, but didn't work,
because I get the values dynamicly

class Mother
{
public:
    Mother(){}
    ~Mother(){}

    template<int msg> inline void HandleMessage(){};
    
    template<> inline void HandleMessage<10>()
    {
        std::cout<<"It works!\n";
    }
};
mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

Hi I have a problem, I hope you can help me with.

I have a number, which I need to compare to some values.

I do not wish to go through all the values, and compare them individually, as I there are too many for it to be efficient.

Neither do I want to create an array with the size of the range of numbers, as it can anything from 0 UINT_MAX

So I am hoping that you might have a better solution.

Here is an example of what I have tried, but didn't work, because I get the values dynamicly

class Mother
{
public:
    Mother(){}
    ~Mother(){}

    template<int msg> inline void HandleMessage(){};
    
    template<> inline void HandleMessage<10>()
    {
        std::cout<<"It works!\n";
    }
};


I think you'll have to be a bit more specific about what you're after, what needs to be compared, stuff like that. I don't know what to make of the code snippet, as it has no comparisons. off the top of my head, if you have ordered data, use a binary search so you don't have to compare every element, but again, without having any idea what kind of data you have or what kind of comparisons you need to make, it's hard to speculate.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Well it is an integer, compared to a list of integers.

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 
Well it is an integer, compared to a list of integers.

Any reason a binary search won't work?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Too time consuming. I would rather not do it at all then

And about the code snippet, if I create an instance of Mother,
and call HandleMessage<10> it would print "It works!\n".

But if I call it with any other value it would do nothing,
the reason I am not doing this, is that I don't know the values at compile time.

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

Too time consuming. I would rather not do it at all then

And about the code snippet, if I create an instance of Mother, and call HandleMessage<10> it would print "It works!\n".

But if I call it with any other value it would do nothing, the reason I am not doing this, is that I don't know the values at compile time.

Care to give a little more background?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Ok I am building a GUI Framework, where there is a Widget class,
which can both have mother widgets and be a mother widget.

When some event happens, the widget receives a message, in the form of an integer value(win32).
I want the widgets to be able to say to their children "I want to know when this happens", and then the mother widget's mother to be able do the same.

The alternative to this subscribe behavior, is to send some of the messages to the widget's mother

mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 12
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You