istream は「空白」を区切り文字として扱います。ロケールを使用して、どの文字が空白であるかを伝えます。facet
ロケールには、文字タイプを分類する ctype が含まれています。このようなファセットは次のようになります。
#include <locale>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>
class my_ctype : public
std::ctype<char>
{
mask my_table[table_size];
public:
my_ctype(size_t refs = 0)
: std::ctype<char>(&my_table[0], false, refs)
{
std::copy_n(classic_table(), table_size, my_table);
my_table['-'] = (mask)space;
my_table['\''] = (mask)space;
}
};
そして、それが動作することを示す小さなテスト プログラム:
int main() {
std::istringstream input("This is some input from McDonald's and Burger-King.");
std::locale x(std::locale::classic(), new my_ctype);
input.imbue(x);
std::copy(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
結果:
This
is
some
input
from
McDonald
s
and
Burger
King.
istream_iterator<string>
>>
ストリームから個々の文字列を読み取るために使用するため、それらを直接使用しても同じ結果が得られるはずです。含める必要があるのは、ロケールの作成とimbue
、ストリームでそのロケールを使用するための使用です。