Is this a functor?

U

Universe

Hello, everyone. I've read a strange declaration from Chapter 7.3, Accelerated C++. The default parameter split seems a normal function. What passes in the function xref via the parameter vector<string> find_words(const string&)? Is it a functor? Or function pointer? Or anything else?

Declaration:

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

Implementation:

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.h

#ifndef GUARD_split_h
#define GUARD_split_h

#include <vector>
#include <string>
std::vector<std::string> split(const std::string&);

#endif


split.cpp

#include <cctype>
#include <string>
#include <vector>

#include "split.h"

using std::vector;
using std::string;

using std::isspace;

vector<string> split(const string& s)
{
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;

// 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
ret.push_back(s.substr(i, j - i));
i = j;
}

}
return ret;
}
 
Ö

Öö Tiib

Hello, everyone. I've read a strange declaration from Chapter 7.3, Accelerated C++. The default parameter split seems a normal function. What passesin the function xref via the parameter vector<string> find_words(const string&)? Is it a functor? Or function pointer? Or anything else?

It is function pointer. Functor is object of a type that has operator()
() overloaded so you can use it like function. Generally usage of
functors instead of function pointers allows compiler to optimize
better.
 
V

Victor Bazarov

Hello, everyone. I've read a strange declaration from Chapter 7.3,
Accelerated C++. The default parameter split seems a normal function.
What passes in the function xref via the parameter vector<string>
find_words(const string&)? Is it a functor? Or function pointer? Or
anything else?

Declaration:

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

Implementation:

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.h

#ifndef GUARD_split_h #define GUARD_split_h

#include<vector> #include<string> std::vector<std::string>
split(const std::string&);

#endif


split.cpp

#include<cctype> #include<string> #include<vector>

#include "split.h"

using std::vector; using std::string;

using std::isspace;

vector<string> split(const string& s) { 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;

// 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
ret.push_back(s.substr(i, j - i)); i = j; }

} return ret; }


The *argument declaration*

vector<string> find_words(const string&)

is a function pointer (it could be a function declaration in a different
context, I think). It's a pointer to a function that takes one argument
- a reference to a constant string and returns a vector or strings. The
function 'xref' declaration also has a default value for its second
argument, 'split', which by itself is an expression yielding a pointer
to the existing function with the exact same signature as the one needed
by the 'xref' function.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top