Why last line doesnt work?
ostringstream stream;
int f = 12345;
stream << f;
string st = stream.str();
cout << typeid(st.rbegin()).name() << endl;
cout << typeid(stream.str().rbegin()).name() << endl;
cout << string(st.rbegin(), st.rend()) << std::endl;
cout << string(stream.str().rbegin(), stream.str().rend()) << endl;
ir says: “String iterators incompatible”, but types the same.
Calling the
ostringstream::str()method creates a new string object, therefore you’re using iterators from two different objects.To solve the issue, store the string in a temporary variable:
Which is what you were doing with the
stvariable already.