set::count() wrong ?

Discussion in 'C++' started by jose luis fernandez diaz, Jan 7, 2004.

  1. 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.
    jose luis fernandez diaz, Jan 7, 2004
    #1
    1. Advertising

  2. 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.
    Jacques Labuschagne, Jan 7, 2004
    #2
    1. Advertising

  3. 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.
    Jacques Labuschagne, Jan 7, 2004
    #3
  4. jose luis fernandez diaz

    tom_usenet Guest

    On 7 Jan 2004 02:15:55 -0800, (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
    tom_usenet, Jan 7, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Patel

    function to count number of set bits

    G Patel, Jan 29, 2005, in forum: C Programming
    Replies:
    29
    Views:
    829
  2. Vish
    Replies:
    3
    Views:
    680
    Lawrence Kirby
    Apr 29, 2005
  3. efelnavarro09
    Replies:
    2
    Views:
    914
    efelnavarro09
    Jan 26, 2011
  4. Paul

    Items.Count wrong on partial page

    Paul, Jul 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    104
  5. Greg Willits

    mysql-ruby returns wrong record count

    Greg Willits, Oct 29, 2007, in forum: Ruby
    Replies:
    2
    Views:
    181
    Greg Willits
    Oct 29, 2007
Loading...

Share This Page