Search code examples
c++stlstdstring

Why do i need to add an asterisk?


I have taken this exaple from cplusplus.com. I have this code :

#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it;
  std::cout << '\n';

  return 0;
}

What purpose does the asterisk after "cout<<" have? If i remove that asterisk it gives me error C2679 in VC++, which commonly apears when you forget to include <string>, but in my case i have included <string>.


Solution

  • The asterisk in this case means "dereferencing". That is; don't print the iterator, print what the iterator points to (just like you'd dereference a pointer to print what it points to rather than just printing the value of the pointer).