473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

set::count() wrong ?

Hi,

In the program below:

#include <iostream.h>
#include <set>

int main()
{
const char *ptr_vars[] = {"one"};
const set<const char *> vars_art(ptr_vars,
ptr_vars+sizeof(ptr_vars)/sizeof(*ptr_v
ars));

char *ptr = "one";
char buffer[100];
strcpy(buffer, "one");
cout << vars_art.count(ptr) << endl;
cout << vars_art.count("one") << endl;
cout << vars_art.count(buffer) << endl;

}
Why "vars_art.count(buffer)== 0" ?

Thanks,
Jose Luis.
Jul 22 '05 #1
3 1597
jose luis fernandez diaz wrote:
Hi,

In the program below:

#include <iostream.h>
#include <set>

int main()
{
const char *ptr_vars[] = {"one"};
const set<const char *> vars_art(ptr_vars,
ptr_vars+sizeof(ptr_vars)/sizeof(*ptr_v
ars));

char *ptr = "one";
char buffer[100];
strcpy(buffer, "one");
cout << vars_art.count(ptr) << endl;
cout << vars_art.count("one") << endl;
cout << vars_art.count(buffer) << endl;

}
Why "vars_art.count(buffer)== 0" ?


Because the literal "one" doesn't live in the same memory location as
buffer. It has the same data, but the pointer value is different (and
that's what you're comparing).

Jacques.

Jul 22 '05 #2
Jacques Labuschagne wrote:
jose luis fernandez diaz wrote:
Hi,

In the program below:

#include <iostream.h>
#include <set>

int main()
{
const char *ptr_vars[] = {"one"};
const set<const char *> vars_art(ptr_vars,
ptr_vars+sizeof(ptr_vars)/sizeof(*ptr_v
ars));

char *ptr = "one";
char buffer[100];
strcpy(buffer, "one");
cout << vars_art.count(ptr) << endl;
cout << vars_art.count("one") << endl;
cout << vars_art.count(buffer) << endl;

}
Why "vars_art.count(buffer)== 0" ?


Because the literal "one" doesn't live in the same memory location as
buffer. It has the same data, but the pointer value is different (and
that's what you're comparing).

To elaborate, the line
strcpy(buffer, "one");
copies the data { 'o', 'n', 'e', '\0' } into buffer. The literal "one"
still exists as a seperate string; you now have two copies of that
data. The set stores pointer values. When it counts occurrences it
looks at the value of the pointer, NOT the value of the thing being
pointed to.

HTH,
Jacques.

Jul 22 '05 #3
On 7 Jan 2004 02:15:55 -0800, jo**********************@yahoo.es (jose
luis fernandez diaz) wrote:
Hi,

In the program below:

#include <iostream.h>
#include <iostream>

iostream.h is non-standard.
#include <set>
using namespace std;

int main()
{
const char *ptr_vars[] = {"one"};
const set<const char *> vars_art(ptr_vars,
ptr_vars+sizeof(ptr_vars)/sizeof(*ptr_v
ars));
The above set will use the default < comparison for pointers, which is
a simple pointer comparison (which isn't even legal or portable - you
can only portably < compare pointers to the same object or array). I
think you wanted

const set<string> ...

or you need to write a comparator that uses strcmp.

char *ptr = "one";
char buffer[100];
strcpy(buffer, "one");
cout << vars_art.count(ptr) << endl;
cout << vars_art.count("one") << endl;
cout << vars_art.count(buffer) << endl;

}
Why "vars_art.count(buffer)== 0" ?


Because the value of the pointer "buffer" is different to the value of
the pointer ptr_vars[0]. All of the other string literals are fine
though, since the implementation is sharing the literal "one".

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

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

Similar topics

2
by: Veerle | last post by:
Hello, I have made a first version of the frontpage of my new homepage. I use the small javascript <script type="text/javascript"> var modDate = new Date(document.lastModified); var day =...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
2
by: Ed Murphy | last post by:
Our client's application software requires all stored procedures to have quoted_identifier set a certain way. I've tripped over this a few times and promptly fixed it, but this morning, I had to...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
10
by: nandor.sieben | last post by:
Is there a well tested implementation of a sorted vector container library to replace set containers? I need only something very basic: Use lower_bound and insert to add a new element if it's...
18
by: damezumari | last post by:
I would like to know how many of the visitors to my site has js enabled and how many has it turned off. I haven't found a simple solution searching Google groups so I suggest the following using...
1
by: Gary Wessle | last post by:
Hi can I use if( set.count(key) ) // element exists { // do something. } else if( !set.count(key) ) // it does not exist { // so something else.
1
by: BeeBop | last post by:
Having an issue with the @Count parameter in the following and I'm rather new to this so I may have missed something: Create Procedure AAA As Declare Customer Cursor Fast_Forward For Select...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.