| | |
problem with iterator
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
here is the full code::
here are the statements:
C++ Syntax (Toggle Plain Text)
cout<<(char)toupper(*pos)<<" "; //this works!
C++ Syntax (Toggle Plain Text)
cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
C++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); //and this also doesn't work cout<<*pos<<" ";
here is the full code::
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <list> using namespace std; int main() { list<char> coll; //this is the container for(char c='a'; c<='z'; ++c) coll.push_back(c); list<char>::iterator pos; for(pos=coll.begin(); pos!=coll.end(); ++ipos) { cout<<(char)toupper(*pos)<<" "; //this works! //cout<<toupper(*pos)<<" "; //without cast it doesn't work, why? //*pos=toupper(*pos); //and this also doesn't work //cout<<*pos<<" "; } cout<<endl; cout<<'A'-0<<endl; }
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"
by Robert Frost the "The Road Not Taken"
your code worked and compiled fine for me (besides a typo in your post)
what exactly are you referring to as not working?
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.
*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:
what exactly are you referring to as not working?
C++ Syntax (Toggle Plain Text)
cout<<toupper(*pos)<<" "; //without cast it doesn't work, why?
C++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); //and this also doesn't work cout<<*pos<<" ";
but it compiles and runs fine on my end

but if you are wondering my output from running this:
C++ Syntax (Toggle Plain Text)
A 65 A B 66 B C 67 C . .. . Y 89 Y 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!
thanks for your answer,
my mistake, yes the code compiles but the 3 statements have different bahaviours....
yes, but according to this 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...
So any ideas why the output is different?
•
•
•
•
your code worked and compiled fine for me (besides a typo in your post)
what exactly are you referring to as not working?
•
•
•
•
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.
c++ Syntax (Toggle Plain Text)
//Program creates char d, sets it equal to lowercase letter //Converts it to uppercase and outputs it #include <cctype> #include <iostream> using namespace std; int main() { char d='a'; d=toupper(d); cout<<d; }
the last set of statements {also compiles} but it prints this � character...
c++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); 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"
by Robert Frost the "The Road Not Taken"
•
•
•
•
thanks for your answer,
my mistake, yes the code compiles but the 3 statements have different bahaviours....
yes, but according to thisit should print the character not its ascii value.... I found the code here...c++ Syntax (Toggle Plain Text)
//Program creates char d, sets it equal to lowercase letter //Converts it to uppercase and outputs it #include <cctype> #include <iostream> using namespace std; int main() { char d='a'; d=toupper(d); cout<<d; }
C++ Syntax (Toggle Plain Text)
/* toupper returns an INT value toupper(a) will return 65 the ascii value for an uppercase A */ // the following statement will output the ascii value for the character cout << toupper(a); // the following statement will cast the INT value to the equivalent CHAR value 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)
/* for the following examples the compiler will automagically cast the INT values to CHAR without the use of an explicit cast*/ char mychar = 65; cout << mychar; // will output A char bigchar = 299; 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...
c++ Syntax (Toggle Plain Text)
*pos=toupper(*pos); cout<<*pos<<" ";
So any ideas why the output is different?
Dont forget to spread the reputation to those that deserve!
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}...
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"
by Robert Frost the "The Road Not Taken"
![]() |
Similar Threads
- iterator deref problem (C++)
- Need help writing my own reverse iterator class (C++)
- Hash Table template implementation help (C++)
- iterator based search algorithm help (C++)
- how to find loop (C)
- "cannot resolve symbol"problem (Java)
- List <char *> Problem (C)
- Problem While Processing A String List (C++)
- Problem with pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: [MSVC++]Error, Trying to convert Letter -> #
- Next Thread: how to use windows.h
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






