Results 1 to 10 of 10

Thread: problem with deque<string>::iterator

  1. #1
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default problem with deque<string>::iterator

    hey everyone
    i'm a student of c++ language. I have to implement an RPN calculator. RPN stands for revers polish notation and it's a more simple way o resolve equations. I was debugging the code when a really strange problem(for me) jumped out. I tried resolving (3+2)*5, this equation in RPN looks like this 3 2 + 5 *, my program does this, finds the "+" and erase it from my deque, the iterator goes back one space so know point to 2, finds the 2, he puts it in a variable called tmp and then erase it, the iterator know points at 3, he does tmp+3 and puts the result where the 3 was, and does the same operation with the 2 remaining number 5 5 *.
    but if I pu (3+2)*5+4, everything in my program changes and I can't undestrand why. the problem is that going to resolve the same bit of before 3 2 +, when i erase the + the iterator doesn't point a 2 but at 3. this is my code
    Qt Code:
    1. if(*it=="*"||*it=="+"||*it=="/"||*it=="-")
    2. {
    3. string sign=*it;
    4. output.erase(it),*it--,str=*it;
    5. strcpy(cstr,str.c_str());
    6. tmp=atoi(cstr);
    7. output.erase(it),*it--;
    8. str=*it;
    9. strcpy(cstr,str.c_str());
    10. output.erase(it),*it--;
    11. if(sign=="+")
    12. tmp=tmp+atoi(cstr);
    13. else if(sign=="-")
    14. tmp=atoi(cstr)-tmp;
    15. else if(sign=="*")
    16. tmp=atoi(cstr)*tmp;
    17. else if (sign=="/")
    18. tmp=atoi(cstr)/tmp;
    19. itoa(tmp,cstr,10);
    20. str=cstr;
    21. *it++;
    22. output.insert(it,str);
    23. it=output.begin();
    24. }
    25. *it++;
    To copy to clipboard, switch view to plain text mode 
    can someone help me?
    thanks
    P.S. sorry for my english

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with deque<string>::iterator

    If you have a stack (aka deque) there is no point in using iterators as you should always be grabbing from the top. When solving 3 2 + 5 * you should be taking '*', then requiring two arguments you should ake 5, then take '+' and since it is an operator, make a recursive call to solve the '+' operation taking 3 and 2 in the process.

    Another way is to not put everything on the stack but proceed from the beginning, you have a stream of tokens and you place 3 and 2 and + on the stack, since + is an operator, take it off along with two preceding arguments, calculate the result and push it onto the stack, then continue pushing from the stream until you reach the next operator, etc.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with deque<string>::iterator

    thank you, probably that's a better way for solving the problem, but didn't answer to my question, I want to understand why my code does this strange thing and solve the problem!
    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with deque<string>::iterator

    That's usually what happens if you're modifying the structure you are currently iterating.

    You also have these strange kinds of statements: "*it--". What is it supposed to do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with deque<string>::iterator

    *it-- should set the iterator to point the deque section before the one it was pointing, how should I do?
    you se if I have this deque 3 2 + 5 * and I erase (with mydeque.erase(it)) the + the it points o 5, but i want it pointing to 2 so i do *it-- and i works with this string, but if I have 3 2 + 5 * 4 + or every other sequence with more than 5 characters then when i erase one element, for example he first + it points al ready o 2 and not 5 has it should be, and so doing *it-- I point to 3 and so the result isn't correct. this is my problem
    Last edited by sax94; 29th January 2012 at 16:03.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with deque<string>::iterator

    Quote Originally Posted by sax94 View Post
    *it-- should set the iterator to point the deque section before the one it was pointing
    What's the asterisk for then? it-- would do just fine.

    you se if I have this deque 3 2 + 5 * and I erase (with mydeque.erase(it)) the + the it points o 5, but i want it pointing to 2 so i do *it-- and i works with this string, but if I have 3 2 + 5 * 4 + or every other sequence with more than 5 characters then when i erase one element, for example he first + it points al ready o 2 and not 5 has it should be, and so doing *it-- I point to 3 and so the result isn't correct. this is my problem
    If you delete the item the iterator points to, you get unexpected behaviour. First move the iterator where you want it to be and then remove the item. However doing what you do now -- you are making it much harder than it really is. For instance there is no point in using deque, with your approach a regular vector would do and you could use index-based counting instead of iterators.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    sax94 (29th January 2012)

  8. #7
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with deque<string>::iterator

    sorry, hate to ask, could you make me a code example because i'm trying to do what you told me but the results are always the same!
    thank's

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with deque<string>::iterator

    I'm sorry, we're not allowed to give solutions for student projects. We can only push you in the right direction.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with deque<string>::iterator

    ok, then i'll repost my code
    Qt Code:
    1. if(*it=="*"||*it=="+"||*it=="/"||*it=="-")
    2. {
    3. if(*it-->"0"||*it<"9"&&*it-->"0"||*it<"9")
    4. {
    5. *it++,*it++;
    6. string sign=*it;
    7. it--,output.erase(output.begin()+count);
    8. str=*it;
    9. strcpy(cstr,str.c_str());
    10. tmp=atoi(cstr);
    11. it--,output.erase(output.begin()+(count-1)),str=*it;
    12. strcpy(cstr,str.c_str());
    13. output.erase(output.begin()+(count-2));
    14. if(sign=="+")
    15. tmp=tmp+atoi(cstr);
    16. else if(sign=="-")
    17. tmp=atoi(cstr)-tmp;
    18. else if(sign=="*")
    19. tmp=atoi(cstr)*tmp;
    20. else if (sign=="/")
    21. tmp=atoi(cstr)/tmp;
    22. itoa(tmp,cstr,10);
    23. str=cstr;
    24. output.insert(it,str);
    25. it=output.begin();
    26. }
    To copy to clipboard, switch view to plain text mode 
    thank's for you help and your patience

  11. #10
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem with deque<string>::iterator

    problem solved, thanks!

Similar Threads

  1. Replies: 9
    Last Post: 23rd December 2011, 15:56
  2. Strange Qt 4.4.0 string problem
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2008, 06:46
  3. QTextFrame::iterator richtext QTextDocument problem?
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2007, 01:02
  4. Problem with std::string's
    By Rayven in forum General Programming
    Replies: 1
    Last Post: 17th July 2007, 18:29
  5. vector iterator as pointer problem
    By Teerayoot in forum General Programming
    Replies: 3
    Last Post: 6th May 2007, 20:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.