2016-04-27 18 views
-3

ファイルから行内の整数を読みたい。C++で行から整数を読み取る方法は?

たとえば行は次のとおりです。3月2日+ 5-5

私は>>を使用する必要があると思うが、それが原因で文字の停止しました。

私は他の機能も使用しようとしていますが、それらはすべて文字用です。

+1

これは簡単な方法ではありません。私は全体の文字列を読み、それをトークン化することをお勧めします。 – Fang

答えて

2

@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 
関連する問題