Thread: boost simple program problem

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    [FIXED] boost simple program problem

    I have this small boost program ...
    Code:
    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
    //    using namespace boost::lambda;
    //    typedef std::istream_iterator<int> in;
    
        std::for_each(
            std::istream_iterator<int> in(std::cin),
            in(),
            std::cout << (boost::lambda::_1 * 3) << " " 
        );
        return 0;
    }
    and when compiling under MSVC .Net 2003, it gives following errors:
    Compiling...
    test-bed.cpp
    c:\my\src\cpp\test-bed\test-bed.cpp(267) : error C2275: 'std::istream_iterator<_Ty>' : illegal use of this type as an expression
    with
    [
    _Ty=int
    ]
    c:\my\src\cpp\test-bed\test-bed.cpp(267) : error C2146: syntax error : missing ')' before identifier 'in'
    c:\my\src\cpp\test-bed\test-bed.cpp(269) : error C2059: syntax error : ')'
    Can someone explain this?
    Last edited by manav; 05-06-2008 at 07:30 AM. Reason: fixed now!

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Well, i solved the errors, this is my new code:
    Code:
    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        using namespace boost::lambda;
        using namespace std;
        typedef std::istream_iterator<int> in;
    
        cout << "Enter numbers, to stop enter Ctrl-Z:" << endl;
        int sum=0;
        std::for_each(
            in(cin),
            in(),
            sum += _1 
        );
        cout << "Sum: " << sum << endl;
        return 0;
    }
    As can be seen, I can calculate the sum now, how to calculate the average here?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you need to do an element count, too, so
    Code:
    std::for_each(in(cin), in(), (sum += _1, ++count));
    But I'm not sure if lambda actually overloads the comma operator.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I changed my code, as CornedBee told:
    Code:
    #include <boost/lambda/lambda.hpp>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main()
    {
        using namespace boost::lambda;
        using namespace std;
        typedef std::istream_iterator<int> in;
    
        cout << "Enter numbers, to stop enter Ctrl-Z:" << endl;
        int sum=0, cnt=0;
        cout << for_each(
            in(cin),
            in(),
            (sum += _1,cnt++)
        );
        cout << "Sum: " << sum << " Average: " << (sum/cnt) << endl;
        return 0;
    }
    but does not work:
    Code:
    Input:
    1 2 3 4 5
    Output:
    Sum: 15 Average: 15

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ah. , is overloaded, but the right side is not a lambda expression.

    Try
    Code:
    (sum += _1, ++boost::lambda::var(cnt))
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Works!
    Thanks CornedBee.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    How about just:

    Code:
    sum = std::accumulate(in(cin), in());

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Won't give you the count.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    Won't give you the count.
    Ehh.. I'm not paying attention.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  3. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  4. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM