when we do a string::begin() in the visual studio IDE, it returns an iterator pointing to the first character in the string. But, when we type string::begin() in the immediate window (or Watch window), it returns the entire string. Why is it so? The immediate window should not behave this way, since string::begin() returns an iterator, and not a string.
Following is the code in Visual Studio IDE:
int main()
{
string s = "abc";
string::iterator it = s.begin();
cout << *it << endl;
return 0;
}
Following is the output: a
But, if we type s.begin() in the immediate window, we get the following output:
s.begin()
"abc"
[ptr]: 0x0133fd08 "abc"
[Raw View]: {...}
The result of s.begin() in the immediate window should be: It should output an iterator pointing to the first element in the string (that is, it should output an iterator pointing to 'a'). But s.begin() outputs the entire string in the immediate window. Why is it so?
This is because the debugger is trying to be helpful. ptr in the iterator is going to be a char* and normally when you have a char*, you have a c-string and you want to see the entire contents of the string, not just what the pointer points to. This is why you see the full contents instead of just a. If you viewed begin() + 1 then you would see bc as the contents, since now the pointer points to b.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With