mercredi 20 novembre 2019

C++ string question - how to access string elements?

I have this simple code in C++:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(){
string s;

s[0]='A';
cout<<"s is: "<<s<<"s[0] is: "<<s[0]<<endl;
}

why is s empty but s[0] has value of 'A'? can C++ strings be assigned values to its elements directly via index? Thanks!

Aucun commentaire:

Enregistrer un commentaire