You are trying to acces an element whhich hasn't existed yet.
a is an empty string so has size of 0, hence a[0] is undefined behavior because the size at least should be 1.
To avoid that use resize() before assigning to a, as follows
#include<iostream>
#include<string>
using namespace std;
int main(){
string a="";
a.resize(3);
a[0]='a';
a[1]='b';
a[2]='c';
cout<<"length of a: "<< a.length();
}
Or use push_back, as follows
#include<iostream>
#include<string>
using namespace std;
int main(){
string a="";
a.push_back('a');
a.push_back('b');
a.push_back('c');
cout<<"length of a: "<< a.length();
}
The first solution is better than the second to avoid reallocating.
Also see this Why is "using namespace std;" considered bad practice?