Iterators and functors

Discussion in 'C++' started by tryptik@gmail.com, Mar 6, 2007.

  1. Guest

    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
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. Guest

    On Mar 6, 3:20 pm, "" <> 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;}
    >
    > };
    >
    > 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
     
    , Mar 6, 2007
    #2
    1. Advertising

  3. 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<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
     
    John Harrison, Mar 6, 2007
    #3
  4. 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<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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 6, 2007
    #4
  5. Jim Langston Guest

    <> wrote in message
    news:...
    > 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.

    >
    > 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
    >
     
    Jim Langston, Mar 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael

    functors and stl find_if

    Michael, Feb 19, 2005, in forum: C++
    Replies:
    4
    Views:
    6,321
    SnaiL
    Feb 19, 2005
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. Christopher
    Replies:
    4
    Views:
    637
    James Kanze
    Nov 29, 2007
  4. , India
    Replies:
    10
    Views:
    1,103
    James Kanze
    Aug 8, 2009
  5. Robert Fendt

    OpenMP and functors

    Robert Fendt, Feb 6, 2010, in forum: C++
    Replies:
    1
    Views:
    348
    Robert Fendt
    Feb 6, 2010
Loading...

Share This Page