943,856 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2064
  • C++ RSS
Oct 4th, 2007
0

problem with iterator

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. cout<<(char)toupper(*pos)<<" "; //this works!
C++ Syntax (Toggle Plain Text)
  1. cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
C++ Syntax (Toggle Plain Text)
  1. *pos=toupper(*pos); //and this also doesn't work
  2. cout<<*pos<<" ";

here is the full code::
c++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Oct 4th, 2007
0

Re: problem with iterator

your code worked and compiled fine for me (besides a typo in your post)

what exactly are you referring to as not working?


C++ Syntax (Toggle Plain Text)
  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.



C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Oct 5th, 2007
0

Re: problem with iterator

thanks for your answer,

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....


Quote ...
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
c++ Syntax (Toggle Plain Text)
  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...
c++ Syntax (Toggle Plain Text)
  1. *pos=toupper(*pos);
  2. cout<<*pos<<" ";

So any ideas why the output is different?
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Oct 5th, 2007
0

Re: problem with iterator

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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 5th, 2007
1

Re: problem with iterator

Click to Expand / Collapse  Quote originally posted by n.aggel ...
thanks for your answer,


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




yes, but according to this
c++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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


Quote ...
the last set of statements {also compiles} but it prints this � character...
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Oct 6th, 2007
0

Re: problem with iterator

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.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [MSVC++]Error, Trying to convert Letter -> #
Next Thread in C++ Forum Timeline: Statically modifying a dll?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC