Thread: Is is possible to specify the ordering of a std::map ?

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, an operator has precedence, but that precedence is not unique, so looping through the operators by precedence is not enough. You would need to map a precedence value to multiple operators. An array of vectors would probably be the best choice.

    However, I doubt this is the best way to evaluate an expression, because you can have parenthesis, which can force lower priority operators to be evaluated first.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by manasij7479 View Post
    That would mean no iteration through it.
    But it could potentially alter my design for the better if I can design the whole thing to not need any.
    You absolutely can iterate through an unordered_map. unordered_map by definition uses a forward iterator.

  3. #18
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    Yeah, an operator has precedence, but that precedence is not unique, so looping through the operators by precedence is not enough. You would need to map a precedence value to multiple operators. An array of vectors would probably be the best choice.

    However, I doubt this is the best way to evaluate an expression, because you can have parenthesis, which can force lower priority operators to be evaluated first.
    I'm just assigning the values manually now.
    As for parentheses, when they occour within an expression..other than being an holder for function arguments, I'd splice the list from the opening to the close, evaluating the resulting list as an independent expression, then substituting the list with that result token.

    Quote Originally Posted by whiteflags
    You absolutely can iterate through an unordered_map. unordered_map by definition uses a forward iterator.
    Didn't know...
    I(thought that I) read in the Wikipedia article about associative data structures that it wasn't possible to iterate through hash tables.
    Going back to that article, I found that there was a term "efficiently" present too. So, I think I'd be fine if I do it once in the whole program.
    Last edited by manasij7479; 08-23-2011 at 09:45 PM.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'd much rather you try out unordered_map in this specific instance rather than letting your preconceived notions dominate your choices.

  5. #20
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    I'd much rather you try out unordered_map in this specific instance rather than letting your preconceived notions dominate your choices.
    Would do so.

  6. #21
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    To anyone who sees this after this post:
    I was, by ignorance, unnecessarily trying to replicate(sort of) the task accomplished by the objects in <functional> which I found out(in detail) just now.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The objects in functional are for passing around operators as functions. That doesn't solve your problem of mapping strings to operators. And they may be inferior to a home brewed solution because you may want to store additional information with the function, such as the precedence.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    The objects in functional are for passing around operators as functions. That doesn't solve your problem of mapping strings to operators. And they may be inferior to a home brewed solution because you may want to store additional information with the function, such as the precedence.
    I think I'll design a solution based on inheriting those objects(from the unary_function or binary_function if the others are too restrictive) if the home brewed ideas can't be translated into code nicely .
    In that way I'd be mix the good from both and hopefully avoid possible pitfalls .

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The implementations of those functions are trivial. They are only there to make it easy to pass operators to standard library algorithms. If your problem is more complex than that, I would not use them.

    For instance, the functions in functional are all templates take two like typed operands. You seem to be writing an interpreter. Unless the language interpreted is like Lua, which only has one arithmetic type, then a container of those function objects isn't going to do it for you.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    The implementations of those functions are trivial. They are only there to make it easy to pass operators to standard library algorithms. If your problem is more complex than that, I would not use them.

    For instance, the functions in functional are all templates take two like typed operands. You seem to be writing an interpreter. Unless the language interpreted is like Lua, which only has one arithmetic type, then a container of those function objects isn't going to do it for you.
    Ok ..then I'm going to design how my Operators are going to work, almost from scratch, because my current design which wasn't done carefully is quite messy.

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    That's why it's usually a bad idea to jump strait into coding. Design first, code later. It is not unusual for design to take the greater part of the project time. Between that and debugging, time spend coding can be quite small.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #27
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    That's why it's usually a bad idea to jump strait into coding. Design first, code later. It is not unusual for design to take the greater part of the project time. Between that and debugging, time spend coding can be quite small.
    Well, the design(here) took place when I was bored during a particularly uncomfortable train journey.
    I'm quite sorry now that I did not take the time to express it on paper with diagrams and such.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ordering characters
    By 12thman in forum C Programming
    Replies: 3
    Last Post: 11-02-2010, 07:15 AM
  2. key ordering in map
    By m37h0d in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 08:28 AM
  3. ListView ordering
    By kidburla in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2006, 03:48 PM
  4. Ordering Queue
    By Japatron in forum C Programming
    Replies: 3
    Last Post: 05-09-2006, 06:51 PM
  5. Ordering names
    By unixOZ in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 05:54 PM