【发布时间】:2017-05-16 10:37:25
【问题描述】:
将从文本文件中提取的行添加到 std::vector<:string> 中的常用方法,其中向量的每个元素都对应于文件的行,类似于以下示例:
https://stackoverflow.com/a/8365024/7030542
std::string line;
std::vector<std::string> myLines;
while (std::getline(myfile, line))
{
myLines.push_back(line);
}
也可以
https://stackoverflow.com/a/12506764/7030542
std::vector<std::string> lines;
for (std::string line; std::getline( ifs, line ); /**/ )
lines.push_back( line );
是否存在一种最有效的方法来做到这一点,比如避免使用辅助字符串?
【问题讨论】:
-
两种解决方案都很好而且效率很高
-
这两种解决方案完全等价。