CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    May 2005

    Question Finding items (strings) in multiple lists

    It's hard to give proper title for this topic but anyways..

    Im working on a script compiler and i need to handle different types of data.
    Actually different categories of items.

    Let's say i have two categories: cat's and bird's. They are different and stored in different lists.
    And let's say there is a simple command: GIVE_FOOD_TO(animal_type, food_type)
    Animal type here can be either from birds category or cat's category.

    And also let's say user gives command: GIVE_FOOD_TO(cat1, fish)
    and also for example: GIVE_FOOD_TO(bird1, birdfood)

    Variable names could be anything, but im storing each variable name in std::map so later i can figure out with what animal current command is used. You get the idea hopefully.

    When im parsing the script then i must know if user supplied either cat or bird.
    If i just would have cat's or bird's category then i would have only 2 lists and not a big problem to loop through either cats list or birds list and find out in what list the "cat1" is or in what particular list the "bird1" is. It would be in one or another.

    But i have a lot of categories and looping through all of these lists (or std::maps) is slow and doesn't seem like a good idea. Just to find out in what list it's stored.
    I can't rely on variable names, they could be anything.

    So i need the ideas how to solve such a problem.

    Big picture atm:

    1) I have one BIG box which stores all of the categories
    2) When i need to find out to which category the variable (animal_type) point's to i must get it quickly, dunno, std::map in std::map or something?

    Any ideas?

    What i need basically is:
    I have different lists (each one is just a category for either birds or cats in this example)
    And when i have variable name, i must find out quickly in what particular category this item is stored. So i can work with it.

    If it's confusing then i will try to explain more.

    Thanks in advance for any tips, hints, ideas.
    Last edited by BytePtr; March 12th, 2013 at 04:31 PM.
    Rate my post if i it was useful!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003

    Re: Finding items (strings) in multiple lists

    Why not map the name to a list?

    Also, apostrophes aren't used to make words plural.

  3. #3
    Join Date
    May 2005

    Re: Finding items (strings) in multiple lists

    Actually yes, i was thinking about this a little bit today and decided to store all variables in one std::map.
    This allows me to also detect re-declared variables.
    Variable will be key and value will be the index of current variable.
    Each time parser will find variable, it will increment the index and that will be stored in map as value. This allows me to get variable index by variable name which is what i need.

    For type checking i just do something else, i don't know yet. have to think about this.
    Rate my post if i it was useful!

  4. #4
    Join Date
    Jul 2005

    Re: Finding items (strings) in multiple lists

    Quote Originally Posted by BytePtr View Post
    Actually yes, i was thinking about this a little bit today and decided to store all variables in one std::map.
    Any particular reason why you need to have the variables sorted? Otherwise, it's better to use an std::unordered_map.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Finding items (strings) in multiple lists

    value will be the index of current variable
    The map value could be a struct/class containing the index and type info + ... as needed - or indeed the key of another map.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.