set::count() wrong ?

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

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.
 
J

Jacques Labuschagne

jose said:
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.
 
J

Jacques Labuschagne

Jacques said:
jose said:
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.
 
T

tom_usenet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top