Why doesn't this program work as expected?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Buell

    Why doesn't this program work as expected?

    #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()(cons t 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.beg in(), 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
    --


    "Honestly, what can I possibly say to get you into my bed?" - Anon.
  • Mark P

    #2
    Re: Why doesn't this program work as expected?

    Ready to kick yourself?

    Alex Buell wrote:[color=blue]
    > #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()(cons t 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.beg in(), v2.begin(), lt2);[/color]

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

    -Mark
    [color=blue]
    > 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[/color]

    Comment

    • Alex Buell

      #3
      Re: Why doesn't this program work as expected?

      On Thu, 23 Feb 2006 23:17:00 GMT Mark P
      <usenet@fall200 5REMOVE.fastmai lCAPS.fm> waved a wand and this message
      magically appeared:
      [color=blue][color=green]
      > > int count1 = count_if(v2.beg in(), v2.begin(), lt2);[/color]
      >
      > Hmm, begin() to begin()... :)[/color]

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

      --


      "Honestly, what can I possibly say to get you into my bed?" - Anon.

      Comment

      • Alf P. Steinbach

        #4
        Re: Why doesn't this program work as expected?

        * Alex Buell:[color=blue]
        > #include <vector>
        > #include <algorithm>
        > #include <iostream>
        >
        > using namespace std;
        >
        > template <typename T>
        > class LessThan[/color]

        Are you aware that the standard library provides std::less and
        std::bind2nd, which together gives you this functionality?
        [color=blue]
        > {
        > private:
        > T comp;
        > public:
        > LessThan(T val) : comp(val) {}
        > void setComp(const T& val) { comp = val; }
        > T getComp(void) { return comp; } const[/color]

        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'.
        [color=blue]
        > bool operator()(cons t T& value) { return value < comp; }[/color]

        Should ideally be declared as 'const'.
        [color=blue]
        > };[/color]

        [color=blue]
        >
        > 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);[/color]

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

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

        [color=blue]
        > double dal = 100.0;
        > LessThan<double > lt2(dal);
        >
        > int count1 = count_if(v2.beg in(), 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?[/color]

        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?

        Comment

        • Alex Buell

          #5
          Re: Why doesn't this program work as expected?

          On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <alfps@start.no >
          waved a wand and this message magically appeared:
          [color=blue][color=green]
          > >
          > > 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);[/color]
          >
          > The array 'darr' should ideally be declared 'const'.
          >
          > Ditto for the vector 'v2', unless you plan on modifying the values.[/color]

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

          --


          "Honestly, what can I possibly say to get you into my bed?" - Anon.

          Comment

          • Alf P. Steinbach

            #6
            Re: Why doesn't this program work as expected?

            * Alex Buell:[color=blue]
            > On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <alfps@start.no >
            > waved a wand and this message magically appeared:
            >[color=green][color=darkred]
            >>> 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);[/color]
            >> The array 'darr' should ideally be declared 'const'.
            >>
            >> Ditto for the vector 'v2', unless you plan on modifying the values.[/color]
            >
            > Is there a good reason to declare them as const?[/color]

            Yes.

            [color=blue]
            > Would it result in better optimisations?[/color]

            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?

            Comment

            • Alex Buell

              #7
              Re: Why doesn't this program work as expected?

              On Fri, 24 Feb 2006 00:35:21 +0100 "Alf P. Steinbach" <alfps@start.no >
              waved a wand and this message magically appeared:
              [color=blue][color=green]
              > > Is there a good reason to declare them as const?[/color]
              >
              > Yes.
              >[color=green]
              > > Would it result in better optimisations?[/color]
              >
              > Probably not.
              >
              > The habit of declaring things 'const', whenever possible, results in a
              > much better chance of a correct program.[/color]

              In case someone else comes along and tries to modify the values, no
              doubt, yes?
              [color=blue]
              > If the program doesn't need to be correct you can make it arbitrarily
              > fast... ;-)[/color]

              lol, that's true.

              --


              "Honestly, what can I possibly say to get you into my bed?" - Anon.

              Comment

              Working...