Iterators and functors

T

tryptik

Hello all,

I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:

class Functor {
std::string operator()(const std::string& s) {/*manipulate
string*/; return newString;}
};

Now, I want to call the functors on a string argument. I write
something like this:

std::vector<Functor> vec;

/*Omitted creation and push_back of a couple Functors */

std::vector<Functor>::const_iterator it = vec.begin();
std::vector<Functor>::const_iterator end = vec.end();

std::string arg("Test argument");

for(; it != end; it++)
std::cout << *it(arg) << "\n"; //Why doesn't this work?

Instead of using the de-reference operator '*', I have to write the
following:

it->operator()(arg);

Can some kind soul explain to me why this is? I prefer the cleaner
syntax of the first statement.

-tryptik
 
T

taylor.hugg

Hello all,

I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:

class Functor {
std::string operator()(const std::string& s) {/*manipulate
string*/; return newString;}

};

Now, I want to call the functors on a string argument. I write
something like this:

std::vector<Functor> vec;

/*Omitted creation and push_back of a couple Functors */

std::vector<Functor>::const_iterator it = vec.begin();
std::vector<Functor>::const_iterator end = vec.end();

std::string arg("Test argument");

for(; it != end; it++)
std::cout << *it(arg) << "\n"; //Why doesn't this work?

Instead of using the de-reference operator '*', I have to write the
following:

it->operator()(arg);

Can some kind soul explain to me why this is? I prefer the cleaner
syntax of the first statement.

-tryptik

well that is a hard question to anser but i think it is wrong
 
J

John Harrison

Hello all,

I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:

class Functor {
std::string operator()(const std::string& s) {/*manipulate
string*/; return newString;}

Functors should be const

std::string operator()(const std::string& s) const
{
...
}
};

Now, I want to call the functors on a string argument. I write
something like this:

std::vector<Functor> vec;

/*Omitted creation and push_back of a couple Functors */

std::vector<Functor>::const_iterator it = vec.begin();
std::vector<Functor>::const_iterator end = vec.end();

std::string arg("Test argument");

for(; it != end; it++)
std::cout << *it(arg) << "\n"; //Why doesn't this work?

Instead of using the de-reference operator '*', I have to write the
following:

it->operator()(arg);

Can some kind soul explain to me why this is? I prefer the cleaner
syntax of the first statement.

-tryptik

Just a question of operator precedence I think, try this.

std::cout << (*it)(arg) << "\n";

john
 
V

Victor Bazarov

I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:

class Functor {
std::string operator()(const std::string& s) {/*manipulate
string*/; return newString;}
};

Now, I want to call the functors on a string argument. I write
something like this:

std::vector<Functor> vec;

/*Omitted creation and push_back of a couple Functors */

std::vector<Functor>::const_iterator it = vec.begin();
std::vector<Functor>::const_iterator end = vec.end();

std::string arg("Test argument");

for(; it != end; it++)
std::cout << *it(arg) << "\n"; //Why doesn't this work?

What do you mean by "doesn't work"? Does it compile?
Instead of using the de-reference operator '*', I have to write the
following:

it->operator()(arg);

Can some kind soul explain to me why this is? I prefer the cleaner
syntax of the first statement.

Use

(*it)(arg);

And read your favourite C++ book again about the precedence of operators.

V
 
J

Jim Langston

Hello all,

I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:

class Functor {
std::string operator()(const std::string& s) {/*manipulate
string*/; return newString;}
};

Now, I want to call the functors on a string argument. I write
something like this:

std::vector<Functor> vec;

/*Omitted creation and push_back of a couple Functors */

std::vector<Functor>::const_iterator it = vec.begin();
std::vector<Functor>::const_iterator end = vec.end();

std::string arg("Test argument");

for(; it != end; it++)
std::cout << *it(arg) << "\n"; //Why doesn't this work?

As stated by others, this is actually:
*(it(arg))
But what you really want is
(*it)(arg)
so you have to specify that.

I now always use (*it) when derefernecing iterators for any context.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top