I tried the below program :
#include <iostream>
#include <string>
int main ()
{
std::string str;
str[0] = 'o';
str[1] = 'k';
std::cout << str.length();
std::cout << "as a whole :";
std::cout << str << std::endl;
std::cout << "character by character :";
std::cout << str[0] << str[1] << std::endl;
return 0;
}
I don't understand why I can't print the string as a whole using the object variable and why the length is returning as 0 since clearly I have added the characters using subscript operator as that would return char reference so I know that is legal.
In addition, I didn't get any kind of exception. So there's that. Obviously, there is lot happening behind the scenes in the string class and I know I am missing something. Could someone help me on this?. Thanks in advance
Aucun commentaire:
Enregistrer un commentaire