473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2215
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.