这个块定义了什么?

时间:2018-03-20 16:00:19

标签: c++

我正在学习Andrew Koenig和Barbara Moo的Accelerated C++(C ++ 98,而不是C ++ 11)中的向量。在这段代码中......

map<string, vector<int>> xref(istream& in, vector<string> find_words(const string&) = split) { ...

......块中定义了什么? xreffind_words?在我的调试器中,调用堆栈为:main() > xref() > split()find_words未在别处定义。

// find all the lines that refer to each word in the input
map<string, vector<int> > xref(istream& in, vector<string> find_words(const string&) = split) {
        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 = find_words(line);

                // 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;
}

此外,split看起来像这样:

vector<string> split(const string& s) { ... }

3 个答案:

答案 0 :(得分:4)

map<string, vector<int>> xref(
    istream& in, 
    vector<string> find_words(const string&) = split
) { /* ... */ }

这定义了名为xref的函数。根据{{​​3}},xref是一个函数:

  • 作为参数:

    1. istream引用(istream& in
    2. 一个函数:(vector<string> find_words(const string&) = split
      • 作为参数:
        1. string常量引用(const string&
      • 返回vector<string>
      • ,其默认值为split= split
  • 并返回map<string, vector<int>>

答案 1 :(得分:0)

xref是定义的函数,find_words是其参数之一(以const string&为参数并返回vector<string>)和{{1的函数}是该参数的默认值。

答案 2 :(得分:0)

xref正在代码块中定义,并返回字符串的映射和int的向量。它通过引用接受istream的参数,该函数接受字符串常量引用并返回默认值被拆分的向量。