问题来源-Accelerated C++,问题8-5
我编写了一个小程序来检查字符串输入的行数,并计算单词在给定行中出现的次数。以下代码实现了这一点:
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <cctype>
#include <iterator>
using std::vector; using std::string;
using std::cin; using std::cout;
using std::endl; using std::getline;
using std::istream; using std::string;
using std::list; using std::map;
using std::isspace; using std::ostream_iterator;
using std::allocator;
inline void keep_window_open()
{
cin.clear();
cout << "Please enter EOF to exit\n";
char ch;
cin >> ch;
return;
}
template <class Out>
void split(const string& s, Out os)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters `['original value of `i', `i)'
while (i != s.size()) {
// ignore leading blanks
// invariant: characters in range `['original `i', current `i)' are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range `['original `j', current `j)' is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j) {
// copy from `s' starting at `i' and taking `j' `\-' `i' chars
*os++ = (s.substr(i, j - i));
i = j;
}
}
}
// find all the lines that refer to each word in the input
map<string, vector<int> > xref(istream& in) // works
// now try to pass the template function as an argument to function - what do i put for templated type?
//map<string, vector<int> > xref(istream& in, void find_words(vector<string, typedef Out) = split) #LINE 1#
{
string line;
int line_number = 0;
map<string, vector<int> > ret;
// read the next line
while (getline(in, line)) {
++line_number;
// break the input line into words
vector<string> words; // works // #LINE 2#
split(line, back_inserter(words)); // #LINE 3#
//find_words(line, back_inserter(words)); // #LINE 4# attempting to use find_words as an argument to function
// remember that each word occurs on the current line
for (vector<string>::const_iterator it = words.begin();
it != words.end(); ++it)
ret[*it].push_back(line_number);
}
return ret;
}
int main()
{
cout << endl << "Enter lines of text, followed by EOF (^Z):" << endl;
// call `xref' using `split' by default
map<string, vector<int> > ret = xref(cin);
// write the results
for (map<string, vector<int> >::const_iterator it = ret.begin();
it != ret.end(); ++it) {
// write the word
cout << it->first << " occurs on line(s): ";
// followed by one or more line numbers
vector<int>::const_iterator line_it = it->second.begin();
cout << *line_it; // write the first line number
++line_it;
// write the rest of the line numbers, if any
while (line_it != it->second.end()) {
cout << ", " << *line_it;
++line_it;
}
// write a new line to separate each word from the next
cout << endl;
}
keep_window_open();
return 0;
}
如您所见,split 函数是一个模板函数,可以根据需要处理各种类型的输出迭代器。
当我尝试通过传入模板化的 split 函数作为参数来概括 xref 函数时,我的问题就来了。我似乎无法获得正确的类型。
所以我的问题是,你能否将一个模板函数作为参数传递给另一个函数,如果可以,是否必须在传递之前声明所有类型?还是编译器可以从主体中使用模板化函数的方式推断出类型?
为了演示我得到的错误,注释掉现有的 xref 函数 header ,并取消注释我试图开始工作的备用 header (就在以下注释行下方。)同时注释这些行标记第 2 行和第 3 行并取消注释第 4 行,它试图使用参数 find_words(默认为 split。)
感谢任何反馈!
最佳答案
以下是解决方法。 您可以使用具有静态函数的类
struct split
{
template <class Out>
static apply(const string& s, Out os) {
// include the body of your split function or call to an existing function
}
};
现在,使外部参照通用
template <typename FIND_WORDS>
map<string, vector<int> > xref(istream& in, FIND_WORDS find_words = split())
{
// replace #2 and #3 by
find_words.apply(line, back_inserter(words));
};
关于c++ - 将模板函数作为参数传递给 C++ 中的另一个函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700298/