Please consider this code. I have seen this type of code several times. words
is a local vector. How is it possible to return it from a function?
Can we guarantee it will not die?
std::vector<std::string> read_file(const std::string& path)
{
std::ifstream file("E:\names.txt");
if (!file.is_open())
{
std::cerr << "Unable to open file" << "
";
std::exit(-1);
}
std::vector<string> words;//this vector will be returned
std::string token;
while (std::getline(file, token, ','))
{
words.push_back(token);
}
return words;
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…