473,663 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t 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<Fun ctorvec;

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

std::vector<Fun ctor>::const_it erator it = vec.begin();
std::vector<Fun ctor>::const_it erator 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 2233
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()(cons t 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<Fun ctorvec;

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

std::vector<Fun ctor>::const_it erator it = vec.begin();
std::vector<Fun ctor>::const_it erator 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.c om 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()(cons t std::string& s) {/*manipulate
string*/; return newString;}
Functors should be const

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

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

std::vector<Fun ctorvec;

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

std::vector<Fun ctor>::const_it erator it = vec.begin();
std::vector<Fun ctor>::const_it erator 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.c om 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()(cons t 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<Fun ctorvec;

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

std::vector<Fun ctor>::const_it erator it = vec.begin();
std::vector<Fun ctor>::const_it erator 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.goo glegroups.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()(cons t 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<Fun ctorvec;

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

std::vector<Fun ctor>::const_it erator it = vec.begin();
std::vector<Fun ctor>::const_it erator 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
1734
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. If I try std::bind2nd() with one of these classes, I get an error about a reference to a reference. What's the proper way to declare functors taking const T& parameters so that they play nicely with adapters? Would it be something like the...
4
376
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 begin(); iterator end(); Is that all I need to do?
2
1550
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 have seen in Effective STL, Scott passes normal functions into places that expect functors. I'm not sure how this works, as I thought functors had to be classes.
2
1436
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++. So:
4
2075
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() should it inherit from anything to make it adaptable? I've not heard of anything before which is surprising. If books don't talk about it then its an unanswered question to many people. Maybe its adaptable without inheriting anything, which would...
2
2339
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
1746
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 problem is that its a bit unsatisfactory and I'd like to improve it. In the constructor of TClassA Functors<TClassA, std::string*Functor = new Functors<TClassA, std::string>(this, &TClassA::Display);
4
2743
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 method, operator () that does the comparison. Fine, no problem. What they fail to tell me is, what is the advantage to wrapping some comparison function in a class and calling it operator()? I also read that "functors" are supposed to be a...
7
1641
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 STL's iterator conventions, I could possibly make my code more economic while probably losing little to no efficiency. As an example, an oscillator will have a "phase accumulator." So typically in a loop I will have something like this:
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.