Why doesn't this program work as expected?

Discussion in 'C++' started by Alex Buell, Feb 23, 2006.

  1. Alex Buell

    Alex Buell Guest

    #include <vector>
    #include <algorithm>
    #include <iostream>

    using namespace std;

    template <typename T>
    class LessThan
    {
    private:
    T comp;
    public:
    LessThan(T val) : comp(val) {}
    void setComp(const T& val) { comp = val; }
    T getComp(void) { return comp; } const
    bool operator()(const T& value) { return value < comp; }
    };

    int main()
    {
    double darr[10] = { 234.4, 11.1, 22.2, 500.0, 50.0, 11.1, 12.1,
    600.6, 99.9, 0.0 }; vector<double> v2(darr, darr + 10);

    double dal = 100.0;
    LessThan<double> lt2(dal);

    int count1 = count_if(v2.begin(), v2.begin(), lt2);
    cout << "There are " << count1 << " values less than " << dal
    << endl;

    return 0;
    }

    Expected output should have been "There are 7 values less than 100",
    but it actually comes up with: "There are 0 values less than 100". Why?

    I'm using GCC 3.4.4 on Linux

    Thanks
    --
    http://www.munted.org.uk

    "Honestly, what can I possibly say to get you into my bed?" - Anon.
     
    Alex Buell, Feb 23, 2006
    #1
    1. Advertising

  2. Alex Buell

    Mark P Guest

    Ready to kick yourself?

    Alex Buell wrote:
    > #include <vector>
    > #include <algorithm>
    > #include <iostream>
    >
    > using namespace std;
    >
    > template <typename T>
    > class LessThan
    > {
    > private:
    > T comp;
    > public:
    > LessThan(T val) : comp(val) {}
    > void setComp(const T& val) { comp = val; }
    > T getComp(void) { return comp; } const
    > bool operator()(const T& value) { return value < comp; }
    > };
    >
    > int main()
    > {
    > double darr[10] = { 234.4, 11.1, 22.2, 500.0, 50.0, 11.1, 12.1,
    > 600.6, 99.9, 0.0 }; vector<double> v2(darr, darr + 10);
    >
    > double dal = 100.0;
    > LessThan<double> lt2(dal);
    >
    > int count1 = count_if(v2.begin(), v2.begin(), lt2);


    Hmm, begin() to begin()... :)

    -Mark

    > cout << "There are " << count1 << " values less than " << dal
    > << endl;
    >
    > return 0;
    > }
    >
    > Expected output should have been "There are 7 values less than 100",
    > but it actually comes up with: "There are 0 values less than 100". Why?
    >
    > I'm using GCC 3.4.4 on Linux
    >
    > Thanks
     
    Mark P, Feb 23, 2006
    #2
    1. Advertising

  3. Alex Buell

    Alex Buell Guest

    On Thu, 23 Feb 2006 23:17:00 GMT Mark P
    <> waved a wand and this message
    magically appeared:

    > > int count1 = count_if(v2.begin(), v2.begin(), lt2);

    >
    > Hmm, begin() to begin()... :)


    Doh! No wonder it didn't work as expected! lol, thanks.

    --
    http://www.munted.org.uk

    "Honestly, what can I possibly say to get you into my bed?" - Anon.
     
    Alex Buell, Feb 23, 2006
    #3
  4. * Alex Buell:
    > #include <vector>
    > #include <algorithm>
    > #include <iostream>
    >
    > using namespace std;
    >
    > template <typename T>
    > class LessThan


    Are you aware that the standard library provides std::less and
    std::bind2nd, which together gives you this functionality?

    > {
    > private:
    > T comp;
    > public:
    > LessThan(T val) : comp(val) {}
    > void setComp(const T& val) { comp = val; }
    > T getComp(void) { return comp; } const


    Note that that 'const' actually applies to the 'bool' on the line
    below... 'getComp' should be declared as 'const'. Also, 'comp' would
    IMO be a better name (I don't like 'computeSin' & friends...), and even
    better, som other name than 'comp', e.g. just 'value'.

    > bool operator()(const T& value) { return value < comp; }


    Should ideally be declared as 'const'.

    > };



    >
    > int main()
    > {
    > double darr[10] = { 234.4, 11.1, 22.2, 500.0, 50.0, 11.1, 12.1,
    > 600.6, 99.9, 0.0 }; vector<double> v2(darr, darr + 10);


    The array 'darr' should ideally be declared 'const'.

    Ditto for the vector 'v2', unless you plan on modifying the values.


    > double dal = 100.0;
    > LessThan<double> lt2(dal);
    >
    > int count1 = count_if(v2.begin(), v2.begin(), lt2);
    > cout << "There are " << count1 << " values less than " << dal
    > << endl;
    >
    > return 0;
    > }
    >
    > Expected output should have been "There are 7 values less than 100",
    > but it actually comes up with: "There are 0 values less than 100". Why?


    Perhaps you meant to use begin() and end() instead of begin() and begin()?


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 23, 2006
    #4
  5. Alex Buell

    Alex Buell Guest

    On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <>
    waved a wand and this message magically appeared:

    > >
    > > int main()
    > > {
    > > double darr[10] = { 234.4, 11.1, 22.2, 500.0, 50.0, 11.1, 12.1,
    > > 600.6, 99.9, 0.0 }; vector<double> v2(darr, darr + 10);

    >
    > The array 'darr' should ideally be declared 'const'.
    >
    > Ditto for the vector 'v2', unless you plan on modifying the values.


    Is there a good reason to declare them as const? Would it result in
    better optimisations?

    --
    http://www.munted.org.uk

    "Honestly, what can I possibly say to get you into my bed?" - Anon.
     
    Alex Buell, Feb 23, 2006
    #5
  6. * Alex Buell:
    > On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <>
    > waved a wand and this message magically appeared:
    >
    >>> int main()
    >>> {
    >>> double darr[10] = { 234.4, 11.1, 22.2, 500.0, 50.0, 11.1, 12.1,
    >>> 600.6, 99.9, 0.0 }; vector<double> v2(darr, darr + 10);

    >> The array 'darr' should ideally be declared 'const'.
    >>
    >> Ditto for the vector 'v2', unless you plan on modifying the values.

    >
    > Is there a good reason to declare them as const?


    Yes.


    > Would it result in better optimisations?


    Probably not.

    The habit of declaring things 'const', whenever possible, results in a
    much better chance of a correct program.

    If the program doesn't need to be correct you can make it arbitrarily
    fast... ;-)


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 23, 2006
    #6
  7. Alex Buell

    Alex Buell Guest

    On Fri, 24 Feb 2006 00:35:21 +0100 "Alf P. Steinbach" <>
    waved a wand and this message magically appeared:

    > > Is there a good reason to declare them as const?

    >
    > Yes.
    >
    > > Would it result in better optimisations?

    >
    > Probably not.
    >
    > The habit of declaring things 'const', whenever possible, results in a
    > much better chance of a correct program.


    In case someone else comes along and tries to modify the values, no
    doubt, yes?

    > If the program doesn't need to be correct you can make it arbitrarily
    > fast... ;-)


    lol, that's true.

    --
    http://www.munted.org.uk

    "Honestly, what can I possibly say to get you into my bed?" - Anon.
     
    Alex Buell, Feb 23, 2006
    #7
    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. Keith Patrick
    Replies:
    2
    Views:
    3,669
    Keith Patrick
    Nov 16, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,218
    Smokey Grindel
    Dec 2, 2006
  3. thomas
    Replies:
    23
    Views:
    1,956
    Andrey Tarasevich
    Feb 22, 2008
  4. Angus
    Replies:
    8
    Views:
    322
    James Kanze
    Jun 18, 2009
  5. Mc Lauren Series
    Replies:
    11
    Views:
    964
    BootNic
    Feb 12, 2010
Loading...

Share This Page