Thread: What's the best ray to give a "rank" to characters

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    What's the best ray to give a "rank" to characters

    Basically I'm using a stack class I've made in a project which changes infix mathematical notation "1 + 2 - 3" to post fix notation "123-+" and basically I want to give a ranking order to simulate precedence of the operands. i was thinking enumerations but I haven't used them much and don't think you can have input recognize a '*' as an enumerated operand. and I don't think two enumerations can have the same value. I was thinking of just using a class for each that inheret from a base class, but still think that would be unnecessary. I just can't figure out the best way to rank inputted operations.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Have you considered a map? You can map characters (operators) to a priority that represents their precedence.
    My best code is written with the delete key.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    that makes sense. like have a map<char, int> and have it that way. That makes a lot more sense, thanks for the advice.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Also I was wondering, I made a typedef in my class that is a pointer to other elements in the container. Is there a way to overload the ++ operator for the type I defined?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a way to overload the ++ operator for the type I defined?
    It's a pointer, right? You can't overload operators for built in types.
    My best code is written with the delete key.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    so how would I simulate how the vector class does it, where they do vector::iterator i, while i != mycontainer.end(), i++ etc kind of thing? Would I have to create a class that acts as a wrapper for a pointer and just override the ++ for that class to where it manipulates the underlying pointer?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so how would I simulate how the vector class does it
    Erm, iterators are designed to function like pointers. Provided your pointer points to an array of some sort, you can traverse it in exactly the same way:
    Code:
    template <typename T, int N>
    class MyContainer {
    public:
      typedef T *iterator;
    
      iterator begin() { return &a[0]; }
      iterator end() { return &a[N]; }
    
      // ...
    private:
      T a[N];
    };
    You only need an iterator class if the iterator doesn't follow expected pointer semantics internally, like with a linked list, or if you want an extra level of encapsulation.
    My best code is written with the delete key.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah, I'm using a linked list as a stack, so I would have to use the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM
  4. Replies: 3
    Last Post: 08-03-2002, 01:27 PM
  5. limit the characters using gets function
    By yukon in forum C Programming
    Replies: 6
    Last Post: 10-08-2001, 02:15 PM