-1

I'm learning about vectors in Accelerated C++ (which is C++ 98, not C++11) by Andrew Koenig and Barbara Moo. In this code...

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

...what is being defined in the block? xref or find_words? In my debugger, the call stack goes: main() > xref() > split(). find_words isn't defined elsewhere.

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

Also, split looks like this:

vector<string> split(const string& s) { ... }
mellow-yellow
  • 1,670
  • 1
  • 18
  • 38
  • This could answer your question https://stackoverflow.com/a/47835838/5470596 – YSC Mar 20 '18 at 16:02
  • `map >` is no longer needed, you can use `map>` now, fixed in C++11. – Drise Mar 20 '18 at 16:04
  • aren't you repeating your question: https://stackoverflow.com/questions/49387638/difference-between-vectorstring-x-and-vectorstring-xconst-string-s/49387713#49387713 .... – bolov Mar 20 '18 at 16:08
  • 1
    `find_words` _isn't defined elsewhere_ - but you can **see** where it's called, and you can see it really calls `split` on that line. So you know the line calling `find_words` really calls `split`. Maybe you haven't encountered function pointers before? – Useless Mar 20 '18 at 16:11
  • @bolov, in https://stackoverflow.com/questions/49387638/difference-between-vectorstring-x-and-vectorstring-xconst-string-s/49387713#49387713, I was trying to understand the theory (different definitions of vector), whereas in this question, I'm trying to understand the (book's) implementation of the second one: in a map. – mellow-yellow Mar 20 '18 at 16:16
  • @Useless, "function pointers" are indeed new to me. The book has not (yet) explained them. – mellow-yellow Mar 20 '18 at 16:21
  • 1
    A typedef for the function pointer type would sure make that code a bit more readable. – Sean Burton Mar 20 '18 at 17:15

3 Answers3

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

This defines the function named xref. According to Clockwise Spiral Rule, xref is a function:

  • taking as arguments:

    1. a istream reference (istream& in)
    2. a function: (vector<string> find_words(const string&) = split)
      • taking as argument:
        1. a string constant reference(const string&)
      • returning a vector<string>
      • whose default value is split (= split)
  • and returning a map<string, vector<int>>
YSC
  • 38,212
  • 9
  • 96
  • 149
0

xref is the function that is defined, find_words is one of its parameters (a function taking a const string& as parameter and returning a vector<string>) and split is the default value for that parameter.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

xref is being defined in the code block and returns a map of a string and a vector of ints. It is taking in parameters of an istreamby reference, a function that takes a string constant reference and returns a vector that the default value is split.

ColMath
  • 21
  • 4