I want to confirm what I think I understand about the lifetime of objects which have been push_back()
'ed on an std::vector
. What I read says that the elements in the vector are copies. So, is the usage below OK? Specifically, is the variable s in f2()
a different instance of an std::string
than the one that is push_back()
'ed in f1()
, and thus safe to use?
void f1(std::vector<std::string>* pv) {
std::string s = "hi";
pv->push_back(s);
}
void f2(void) {
std::vector<std::string> v;
f1(&v);
for (size_t i = 0; i < v.size(); ++i) {
std::string s = v.at(i);
std::cout << s;
}
}