473,657 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Feb 23 '06 #1
6 1418
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()(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);
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****@fall200 5REMOVE.fastmai lCAPS.fm> waved a wand and this message
magically appeared:
int count1 = count_if(v2.beg in(), 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()(cons t 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.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?


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
1232
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= { {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'}, {'W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M','Q'},
12
5349
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 manuals for the proper techniques to prepare an sqlj program. When I went to try the sqlj command, I got this: Exception in thread "main" java.lang.NoClassDefFoundError: sqlj/tools/Sqlj
149
25122
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? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
10
1490
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 doesn't help either. Where's the problem? Thanks.
34
26624
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
13602
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
27420
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
1967
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 results at all in an otherwise functioning C# web application program: SELECT * FROM RR_Contacts WHERE FirstName LIKE '*Robert*' While this query works (gives a result set) with either single or double quotes etc etc using Access to execute the...
1
1616
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 is correct, but when I run the tester, it is obviously not. The goal is to enter a base and a time, and find time % base, then tell how many times it cycled back to zero. I don't know what is wrong with my program, because it appears to me that it...
13
5364
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: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.