472,779 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

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 2180
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: red floyd | last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler). I'm writing functors for my classes. Because the objects in my containers are large, I'm making my functors take const T& parameters....
4
by: nsgi_2004 | last post by:
Hello, I made a table class (implemented with an array) and would like to add iterators so that I can use the std algorithms with it. I added this to my class: typedef T* iterator; iterator...
2
by: nsgi_2004 | last post by:
Hi, I have been learning about functors and at first everything was clear. Make a class and overload operator () so that an object of the functor can be thought of as a function. However, I...
2
by: dgront | last post by:
Greetings! I believe someone can spare me a minute and give me some ideas. Once upon a time I had a C program: double propert; char symb; char* descript; But now I want to make it in C++....
4
by: Fraser Ross | last post by:
Functors taking 1 argument for operator() should inherit from unary_function and those with 2 arguments should inherit from binary_function. If a functor has zero arguments for its operator()...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
7
by: Leslie Sanford | last post by:
My area of programming is DSP. I write things like filters, oscillators, envelopes, etc. I've been looking at STL iterators, and what's struck me is that if I can find ways to model my code using...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.