开发者

Logical Programming Problem

I've been trying to solve this problem for quite sometime but I am having trouble with it.

Let's say on a trigger, you receive values.

First trigger: You get 1 
Second trigger: You get 1, 2
Third trigger: You get 1, 2, 3

So, I store 1. For 2nd trigger, I store 2 since 1 already exist. For 3rd trigger, I store 3 since 1,2 already exist

so in total I have stored 1,2,3

As you can see, we can easily check for new values, if old != new.

Here's come the problem:

Fourth trigger: You get 1, 2, 4

For 4th trigger, I store 1, 2 because it exists but how do I check against 3 and remove 3 from store and check if 4 is new?

If you are having problems understanding this, feel free to clarify. Tha开发者_高级运维nks!


Use a std::set<int> container. When a trigger arrives, clear it an insert all the values from trigger. This should be ok if you work with just a few numbers (about ten or so). With more, a little bit more sophisticated approach might be required.


Hard to tell what you're asking exactly, but see std::set data structure if your main problem is trying to maintain a set of unique numbers and efficiently check for existence in the set.


Your logic changed between 1,2,3 and 1,2,4 (only stored 3 on former, but stored 1,2,4 on latter)

In that case, ignore data recv'd that already exists, only storing new data, unless some old data was not sent in which case you'll create a new set of data to store.

But, I'm guessing that's not what you had in mind at all :)

edit
I see it's been edited now, so my answer is invalid

edit-2
the fastest way is to drop all stored data on each iteration as comparisons will take as long (if not longer) than a complete save of sent data.


Your approach sounds like it is better served by using some basic set theory. A couple of answers already point you to STL sets for that matter. With that, you'll need to iterate through the reported values to test for membership in the set.

However, is there an opportunity to affect what is reported with each "trigger"? For example, if this is something like a select poll, you could just put whatever it is that you're polling into a different state so that it is not reported as ready in subsequent triggers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜