I am trying to create a string by accessing its individual index positions and trying to print it.
#include<iostream> #include<string> using namespace std; int main() { string s=""; s[0]='a'; s[1]='b'; s[2]='c'; s[3]='d'; cout << s << endl; for(int i = 0; i < 4; i++) cout << s[i] << endl; return 0; } This doesn't print a entire string but does print its individual characters.
Following code prints both the strings and its individual characters.
#include<iostream> #include<string> using namespace std; int main() { string s=" "; s[0]='a'; s[1]='b'; s[2]='c'; s[3]='d'; cout << s << endl; for(int i = 0; i < 4; i++) cout << s[i] << endl; return 0; } Why does this happen. Links to any further reading are appreciated.