-
April 30th, 2015, 12:41 PM
#1
Return a list of defines given a range.
Hi
I have a bunch of defines with values from 1-100.
I want to provide some sort of function that takes in a range and returns a list or map (like a python dictionary) of items that fall under that range
eg.
#define BOB_AGE 10 // this is how existing defines are and I cant change them.
#define BILL_AGE 13
#define TIM_AGE 22
#define TOM_AGE 30
getPeopleBetween(5,20);
I would want a python like dictionary that is returned, where I can have map like functionality and request the value (age) from this returned list.
At the moment, I have a master map list where I add all defines within the constructor. I was thinking of creating another list and returning that within the get function. However, would I not have scope problems?
Just thinking what would be a good solution.
Hope this makes sense.
Thanks all.
-
April 30th, 2015, 01:09 PM
#2
Re: Return a list of defines given a range.
The problem is that preprocessing directives are executed at an earlier phase in translation. As such, by the time we start talking about containers and other data structures, they no longer exist, but rather you are dealing with what replaced them, e.g., instead of BOB_AGE it is just 10.
Perhaps what you could do is to manually construct a sorted list (e.g., array, perhaps std::array) of BOB_AGE, BILL_AGE, etc. This way, you can do binary search (e.g., using lower_bound and/or upper_bound from <algorithm>) to find the range. Alternatively, you could use std::set.
If you actually need the names like "Bob", "Bill", etc, then the sorted list or set could consist of std:air<int, std::string> objects instead.
-
April 30th, 2015, 03:55 PM
#3
Re: Return a list of defines given a range.
Originally Posted by rudyloo
Just thinking what would be a good solution.
If you are used to functional programming why not use a C++ functional library?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|