Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何读取每行具有不同格式的文件。我有一个看起来像这样的文件
James 0 14 12 Lucy Lucas 0 45 Alice 87 23 10 23 etc...
而且我必须存储这些值以供以后使用。我该怎么做。
如果每一行都有我会使用的相同格式getline(),但我可以在这里使用它吗?
getline()
获得每一行后,您可以使用 astringstream来帮助分离您的值。例如:
stringstream
std::string line = "James 0 14 12"; std::istringstream ss(line); std::string piece; while(ss >> piece) std::cout << piece << '\n';
将打印出:
James 0 14 12
您可以将每个部分添加到您想要的任何数据结构中。