lundi 31 décembre 2018

String input/output

I am kinda new to programming, so pardon me for any stupid questions :P, i noticed that when i write this

int main()
    {
        string s;
        s[0]='a';
        cout<<s.size();
        cout<<s<<" ";
        cout<<s[0];
       return 0;
    }

The output is 0 a , firstly why is the size 0? and why didn't anything get printed when i write cout<<s; but it gives a for cout<<s[0]. if i use push_back it gives normal out put.

int main()
{
    string s;
    s.push_back('a');
    cout<<s.size();
    cout<<s<<" ";
    cout<<s[0];
   return 0;
}

output :- 1a a

I might have overlooked something but i would be really appreciate if some could point out the cause. Thank you.

Aucun commentaire:

Enregistrer un commentaire