problem with iterator

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

problem with iterator

 
0
  #1
Oct 4th, 2007
I just started learning about stl.... the problem is that 3 cases that i consider "equivelant", don't do the same thing...so the question is, why they are not equivalent?

here are the statements:
  1. cout<<(char)toupper(*pos)<<" "; //this works!
  1. cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
  1. *pos=toupper(*pos); //and this also doesn't work
  2. cout<<*pos<<" ";

here is the full code::
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. list<char> coll; //this is the container
  9.  
  10. for(char c='a'; c<='z'; ++c)
  11. coll.push_back(c);
  12.  
  13.  
  14. list<char>::iterator pos;
  15.  
  16. for(pos=coll.begin(); pos!=coll.end(); ++ipos)
  17. {
  18. cout<<(char)toupper(*pos)<<" "; //this works!
  19.  
  20. //cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
  21.  
  22. //*pos=toupper(*pos); //and this also doesn't work
  23. //cout<<*pos<<" ";
  24.  
  25. }
  26. cout<<endl;
  27. cout<<'A'-0<<endl;
  28.  
  29. }
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: problem with iterator

 
0
  #2
Oct 4th, 2007
your code worked and compiled fine for me (besides a typo in your post)

what exactly are you referring to as not working?


  1. cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
while it does work the output from it may not be what you are expecting. the output from toupper is int not char, so when it is cout'ed the result is going to the ascii equivalent for the character.



  1. *pos=toupper(*pos); //and this also doesn't work
  2. cout<<*pos<<" ";
*pos is pointing to the character that the iterator is on (my wild guess) so it can be cast back to char without an explicit cast.

but it compiles and runs fine on my end

but if you are wondering my output from running this:

  1. A 65 A
  2. B 66 B
  3. C 67 C
  4. . .. .
  5. Y 89 Y
  6. Z 90 Z
Last edited by Killer_Typo; Oct 4th, 2007 at 6:32 pm.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: problem with iterator

 
0
  #3
Oct 5th, 2007
thanks for your answer,

Originally Posted by Killer_Typo View Post
your code worked and compiled fine for me (besides a typo in your post)

what exactly are you referring to as not working?
my mistake, yes the code compiles but the 3 statements have different bahaviours....


cout<<toupper(*pos)<<" ";
while it does work the output from it may not be what you are expecting. the output from toupper is int not char, so when it is cout'ed the result is going to the ascii equivalent for the character.
yes, but according to this
  1. //Program creates char d, sets it equal to lowercase letter
  2. //Converts it to uppercase and outputs it
  3. #include <cctype>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char d='a';
  11. d=toupper(d);
  12. cout<<d;
  13. }
it should print the character not its ascii value.... I found the code here...

the last set of statements {also compiles} but it prints this � character...
  1. *pos=toupper(*pos);
  2. cout<<*pos<<" ";

So any ideas why the output is different?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: problem with iterator

 
0
  #4
Oct 5th, 2007
the code that you posted originally will not compile (line 16 has a typo).
> the last set of statements {also compiles} but it prints this � character...
the most likely cause is that the iterator pos == coll.end() when you dereferenced it.
Last edited by vijayan121; Oct 5th, 2007 at 4:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: problem with iterator

 
1
  #5
Oct 5th, 2007
Originally Posted by n.aggel View Post
thanks for your answer,


my mistake, yes the code compiles but the 3 statements have different bahaviours....




yes, but according to this
  1. //Program creates char d, sets it equal to lowercase letter
  2. //Converts it to uppercase and outputs it
  3. #include <cctype>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char d='a';
  11. d=toupper(d);
  12. cout<<d;
  13. }
it should print the character not its ascii value.... I found the code here...
this statement is entirely different from what you have in your code.

  1. /*
  2. toupper returns an INT value
  3. toupper(a) will return 65 the ascii value for an uppercase A
  4. */
  5.  
  6. // the following statement will output the ascii value for the character
  7. cout << toupper(a);
  8.  
  9. // the following statement will cast the INT value to the equivalent CHAR value
  10. cout << (char) toupper(a);

Notice how i had to cast the output from the toupper function to char to get the character output.

By default the return type of toupper is INT not CHAR so to output the results directly will output an integer value not the character equivalent.

The reason your code works (whether or not it throws a warning) is because there exists an implicit cast from INT to CHAR.

So a user can create a CHAR and assign an int value to it.
  1. /* for the following examples the compiler will automagically cast the INT values to CHAR without the use of an explicit cast*/
  2. char mychar = 65;
  3. cout << mychar; // will output A
  4.  
  5. char bigchar = 299;
  6. cout << bigchar; // will output a random char as char can only store a value between 0 and 255, your compiler may throw a warning as well


the last set of statements {also compiles} but it prints this � character...
  1. *pos=toupper(*pos);
  2. cout<<*pos<<" ";

So any ideas why the output is different?
I'm not exactly sure why that is happening. It is working fine on my end, you might want to step through the code with your debugger and see what exactly is being outputted for that one.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: problem with iterator

 
0
  #6
Oct 6th, 2007
thank you all for your help...

vijayan was right... you see in this program i tested both the const_iterator and the iterator... When i copied the for-loop {from the part where i was testing the const_iterator} i changed the pos to ipos, but i stil used the word pos inside the loop{in the part i wanted to test the iterator}, so when i tried to dereference it, i derefernced the pos iterator which was equal[from the previous loop] to coll.end(), this is why it didn't work for me...

It worked for everyone else because when i posted part of the code here i rechanged ipos to pos{it seemed more reasonable as a var name}...
Last edited by n.aggel; Oct 6th, 2007 at 1:29 pm.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC