472,984 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

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()(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.
Feb 23 '06 #1
6 1382
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

Feb 23 '06 #2
On Thu, 23 Feb 2006 23:17:00 GMT Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> 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.
Feb 23 '06 #3
* 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?
Feb 23 '06 #4
On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <al***@start.no>
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.
Feb 23 '06 #5
* Alex Buell:
On Fri, 24 Feb 2006 00:22:25 +0100 "Alf P. Steinbach" <al***@start.no>
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?
Feb 23 '06 #6
On Fri, 24 Feb 2006 00:35:21 +0100 "Alf P. Steinbach" <al***@start.no>
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.
Feb 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Protoman | last post by:
I'm trying to write an encryption program, and it doesn't produce the right output; "GOOGLE" encrypted w/"GOOGLE" IS NOT "ee". Here's the code: namespace { const char vTable= {...
12
by: Rhino | last post by:
I am having an odd problem: the sqlj command on my system doesn't work. I am running DB2 (LUW) V8 (FP8) on WinXP. I haven't done an sqlj program since Version 6 of DB2 (LUW) so I checked the...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
10
by: sandorf | last post by:
I'm using the Windows version of Python and IDLE. When I debug my .py file, my modification to the .py file does not seem to take effect unless I restart IDLE. Saving the file and re-importing it...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
4
by: Eric Lilja | last post by:
Is this an invalid program? Doesn't compile on my system: #include <cstdio> class Why { enum TArch {LITTLE_ENDIAN, BIG_ENDIAN, NON_IEEE}; TArch Architecture; }; int
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
by: Budsy | last post by:
This is a peculiar problem for me. I am finding that the following query works perfectly and gives the expected result set when used in the Access query design environment, but it simply gives no...
1
by: truedecembr | last post by:
Hi everyone, I am brand new to Java and not really even sure what I'm doing... I'm supposed to be writing a Timer class that is part of a stop watch application, and it seems to me that the program...
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.