So I have the following code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> strs;
strs.push_back("happy");
const string& a1 = strs[0];
strs.push_back("birthday");
const string& a2 = strs[1];
strs.push_back("dude");
const string& a3 = strs[2];
printf("%s\n", a1.c_str());
return 0;
}
which is pretty straightforward but it doesn't work. The printf doesn't print anything. It does print if I change it to:
const string& a1 = strs[0].c_str();
can someone please explain the behavior of it.