如何从 C++ 中的一行中读取整数?

How to read integers from a line in C++?

我想从文件中读取一行中的整数。

例如该行是:3/2+5-5

我想我需要使用>>,但是因为字符的原因它停止了;

其他的功能我也尝试过,但是都是针对字符的。

正如@Fang 已经指出的那样,没有简单的方法可以做到这一点。您可以阅读整行并通过以下代码对其进行标记化:

std::ifstream f("file.txt");

std::string line;
std::getline(f, line);

std::vector<std::string> integers;
boost::split(integers, line, boost::algorithm::is_any_of("+-*/"), boost::token_compress_on);

// Then convert strings from the integers container to ints