如果您需要以逗号精确拆分字符串,我知道的最简单的方法是重新定义流空间的含义。这很容易更换std::ctype<char>
刻面。这是我之前发布的版本...
#include <iostream>
#include <iterator>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
typedef string T; // to simplify, always consider T as string
template<typename input_iterator>
void do_something(const input_iterator& first, const input_iterator& last) {
const ostream_iterator<T> os(cout, "\n");
const set<T> words(first, last);
copy(words.begin(), words.end(), os);
}
#include <locale>
template <char S0, char S1>
struct commactype_base {
commactype_base(): table_() {
std::transform(std::ctype<char>::classic_table(),
std::ctype<char>::classic_table() + std::ctype<char>::table_size,
this->table_,
[](std::ctype_base::mask m) -> std::ctype_base::mask {
return m & ~(std::ctype_base::space);
});
this->table_[static_cast<unsigned char>(S0)] |= std::ctype_base::space;
this->table_[static_cast<unsigned char>(S1)] |= std::ctype_base::space;
}
std::ctype<char>::mask table_[std::ctype<char>::table_size];
static std::ctype_base::mask clear_space(std::ctype_base::mask m) {
return m & ~(std::ctype_base::space);
}
};
template <char S0, char S1 = S0>
struct ctype:
commactype_base<S0, S1>,
std::ctype<char>
{
ctype(): std::ctype<char>(this->table_, false) {}
};
int main() {
std::cin.imbue(std::locale(std::locale(), new ::ctype<',', '\n'>));
const istream_iterator<T> is(cin), eof;
do_something(is, eof);
return 0;
}