template <class write_iter> //class for splitting a line into individual words
class Split
{
public:
split(const string& line, write_iter destination)
{
typedef string::const_iterator iter;
iter i;
while(i != line.end())
{
i = find_if(i, line.end(), not_space);
iter j = find_if(i, line.end(), space);
if (i != line.end())
*destination++ = string(i,j);
i = j;
}
}
bool not_space(char c)
{
return !isspace(c);
}
bool space(char c)
{
return isspace(c);
}
};
int main()
{
while(getline(cin, line))
Split<> split(line, back_inserter(words));
}
我希望能够传递任何我想要的迭代器: ex:back_insterter(somevector),ostream_iterator
对于back_inserter,Split&lt; ?? &GT;分割
答案 0 :(得分:0)
Split <decltype(back_inserter(words))>(line, back_inserter(words));
或者这里是完整的类型:
Split <std::back_insert_iterator<std::vector<std::string>>>(line, back_inserter(words));
为什么不只是有一个模板化的构造函数,而不是一个模板化的类?它会让事情变得更容易。
那些not_space
&amp; space
函数也应该是静态的,find_if
才能正常工作。或者将它们移到课堂外。
还有一件事:back_insert_iterator不需要递增。 *destination = string(i,j);
就足够了。