Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: STL set/map...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 17 Topic 45770 of 47031
Post > Topic >>

Re: STL set/map w.r.t const char*

by S S <sarvesh.singh@[EMAIL PROTECTED] > May 6, 2008 at 04:01 AM

On May 6, 3:34 pm, S S <sarvesh.si...@[EMAIL PROTECTED]
> wrote:
> On May 6, 2:38 pm, "Jim Langston" <tazmas...@[EMAIL PROTECTED]
> wrote:
>
>
>
>
>
>
>
> > S S wrote:
> > > Hi
>
> > > I have a very basic question, but it's a good one. Below is the code
> > > fragment.
>
> > > struct ltstr
> > > {
> > >  bool operator()(const char* s1, const char* s2) const
> > >  {
> > >    return strcmp(s1, s2) < 0;
> > >  }
> > > };
>
> > > int main()
> > > {
> > >  const int N = 6;
> > >  const char* a[N] = {"isomer", "ephemeral", "prosaic",
> > >                      "nugatory", "artichoke", "serif"};
> > >  set<const char*, ltstr> A(a, a + N);
> > >  if (A.find("ephemeral") != A.end())
> > >    cout << "Found";
> > >  else
> > >    cout << "Not found";
> > >  return 0;
> > > }
>
> > > Output will be -> "Found"
> > > My question is , Why?
>
> > > How it is able to compare a const char* with another const char* to
> > > find that value? I did not specify any equality operator? I just
> > > mentioned strcmp(s1, s2) < 0
> > > which means, when strcmp(s1, s2) == 0 (in case of match)
> > > it will return false. So how set/map are able to find the const
char*
> > > value?
>
> > I believe it's undefined behavior.  You have the constant "ephemeral"
in
> > your program twice.  Once for the char array, once for the parameter
of the
> > find.  The compiler probably sees that they are exactly the same, so
instead
> > of making 2 it just makes one and uses the same pointer for both.
>
> > An example of this is this program that on my system outputs:
> > 00416808 00416808
> > one time I run it
>
> > #include <iostream>
>
> > int main()
> > {
> >     const char* foo = "Hello";
> >     const char* bar = "Hello";
>
> >     std::cout << reinterpret_cast<const int*>( foo ) << " " <<
> > reinterpret_cast<const int*>( bar ) << "\n";
>
> > }
>
> > The compiler doesn't bother to create two constant arrays for "Hello",
since
> > it's constant it just creates it once and uses the same address for
both foo
> > and bar.
>
> > --
> > Jim Langston
> > tazmas...@[EMAIL PROTECTED]
>
> Jim,
>
> This way, it has been used extensively in our code, and it all works
> fine. I am not much convinced with your argument, but you may be true.
> But then, how to use find() function?


One more point I wanted to make, same should be the case with map when
we use something like
m["somestring"]
? So what to do in such case where find and above kind of syntax is
undefined???
 




 17 Posts in Topic:
STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 02:28:00 
Re: STL set/map w.r.t const char*
"Jim Langston"   2008-05-06 02:38:48 
Re: STL set/map w.r.t const char*
Ian Collins <ian-news@  2008-05-06 21:45:25 
Re: STL set/map w.r.t const char*
dizzy <dizzy@[EMAIL PR  2008-05-06 12:48:27 
Re: STL set/map w.r.t const char*
Ian Collins <ian-news@  2008-05-06 21:58:20 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 03:34:07 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 03:34:45 
Re: STL set/map w.r.t const char*
Ian Collins <ian-news@  2008-05-06 23:02:48 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 03:38:11 
Re: STL set/map w.r.t const char*
Ian Collins <ian-news@  2008-05-06 23:04:47 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 04:01:55 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 04:21:30 
Re: STL set/map w.r.t const char*
dizzy <dizzy@[EMAIL PR  2008-05-06 15:48:36 
Re: STL set/map w.r.t const char*
James Kanze <james.kan  2008-05-06 05:28:54 
Re: STL set/map w.r.t const char*
terminator <farid.mehr  2008-05-06 07:25:33 
Re: STL set/map w.r.t const char*
S S <sarvesh.singh@[EM  2008-05-06 08:10:32 
Re: STL set/map w.r.t const char*
"Andrew Koenig"  2008-05-07 19:10:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:40:39 CDT 2008.