469,927 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Iterators and functors

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<Functorvec;

/*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

Mar 6 '07 #1
4 2011
On Mar 6, 3:20 pm, "tryp...@gmail.com" <tryp...@gmail.comwrote:
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<Functorvec;

/*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

Mar 6 '07 #2
tr*****@gmail.com wrote:
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<Functorvec;

/*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
Mar 6 '07 #3
tr*****@gmail.com wrote:
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<Functorvec;

/*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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 6 '07 #4
<tr*****@gmail.comwrote in message
news:11**********************@q40g2000cwq.googlegr oups.com...
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<Functorvec;

/*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.
>
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

Mar 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by red floyd | last post: by
4 posts views Thread by nsgi_2004 | last post: by
2 posts views Thread by nsgi_2004 | last post: by
2 posts views Thread by dgront | last post: by
4 posts views Thread by Fraser Ross | last post: by
2 posts views Thread by ma740988 | last post: by
2 posts views Thread by Jon Slaughter | last post: by
4 posts views Thread by Christopher | last post: by
7 posts views Thread by Leslie Sanford | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.