CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2007
    Posts
    97

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: Return a list of defines given a range.

    Quote Originally Posted by rudyloo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured