473,396 Members | 2,004 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,396 software developers and data experts.

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

S S
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*, ltstrA(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?
Jun 27 '08 #1
13 2121
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*, ltstrA(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
ta*******@rocketmail.com
Jun 27 '08 #2
Jim Langston 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*, ltstrA(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.
That does not matter, note the use of the second template argument of
std::set.

--
Ian Collins.
Jun 27 '08 #3
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*, ltstrA(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?
Given an ordered set, A < B < C and a search item X,

if (X < C) and !(B < X) then X is equivalent to B.

--
Ian Collins.
Jun 27 '08 #4
S S
On May 6, 2:38 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
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*, ltstrA(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...@rocketmail.com

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?
Jun 27 '08 #5
S S
On May 6, 2:45 pm, Ian Collins <ian-n...@hotmail.comwrote:
Jim Langston 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*, ltstrA(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.

That does not matter, note the use of the second template argument of
std::set.

--
Ian Collins.
Ian,

You wanted to explain something? I did not get you, sorry. Could you
elaborate please?
Jun 27 '08 #6
S S
On May 6, 3:34 pm, S S <sarvesh.si...@gmail.comwrote:
On May 6, 2:38 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:


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*, ltstrA(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...@rocketmail.com

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???
Jun 27 '08 #7
S S wrote:
On May 6, 2:45 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Jim Langston wrote:
>>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.
>That does not matter, note the use of the second template argument of
std::set.
Please trim and don't quote signatures.
>
Ian,

You wanted to explain something? I did not get you, sorry. Could you
elaborate please?
I was pointing out to Jim that you had provided your own less object, so
there wasn't any UB.

--
Ian Collins.
Jun 27 '08 #8
S S
Dizzy, Ian

May be I am missing something, but my statement
strcmp(s1, s2) < 0
will give me the expression

return 0 < 0;

which is FALSE. Am I correct?

if (0 < 0)
cout << "Its true";
else
cout << "Its false";

It prints, Its false.

Thanks in advance.
Jun 27 '08 #9
On May 6, 11:58 am, Ian Collins <ian-n...@hotmail.comwrote:
S S wrote:
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*, ltstrA(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?
Given an ordered set, A < B < C and a search item X,
if (X < C) and !(B < X) then X is equivalent to B.
The issue in std::set is a bit more complicated, because the
domain may include elements which are not members of std::set.
Basically, set<>::find first locates an element i such that for
all previous elements j, j < x, and i for i, !(i < x) (which
implies that the same holds for all following elements, of
course). Having found this i, if !(x < i) determines whether
the elements are equivalent or not: if !(i < x) && !(x < i), the
elements are considered to be equivalent.

Note the "considered to be". You can easily define an operator<
which is inconsistent with ==. (He has, in fact: his ltstr is
inconsistent with == over char const*. Of course, if he were
also interested in ==, he'd define an eqstr, which returned
strcmp(s1, s2) == 0.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #10
S S wrote:
Dizzy, Ian

May be I am missing something, but my statement
strcmp(s1, s2) < 0
will give me the expression

return 0 < 0;

which is FALSE. Am I correct?
Correct.
>
if (0 < 0)
cout << "Its true";
else
cout << "Its false";

It prints, Its false.
So? I said 2 elements are equivalent iff (!(elem1 < elem2) && !(elem2 <
elem1)). Let's take your example, let's compare "a" and "b" so we have
!(strcmp("a", "b")<0) && !(strcmp("b", "a")<0) which means false && true
which yields false so "a" and "b" are not equivalent, BUT, let's take "a"
and "a" and we have:
!(strcmp("a", "a")<0) && !(strcmp("a", "a")<0) which is "true && true" which
is true so "a" and "a" are equivalent elements acording to your given
predicate (strcmp(x,y) < 0).

--
Dizzy

Jun 27 '08 #11
On May 6, 1:28*pm, S S <sarvesh.si...@gmail.comwrote:
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*, ltstrA(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?
(a==b)==((!(a<b))&&!(b<a))

regards,
FM
Jun 27 '08 #12
S S
On May 6, 5:28 pm, James Kanze <james.ka...@gmail.comwrote:
On May 6, 11:58 am, Ian Collins <ian-n...@hotmail.comwrote:
S S wrote:
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*, ltstrA(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?
Given an ordered set, A < B < C and a search item X,
if (X < C) and !(B < X) then X is equivalent to B.

The issue in std::set is a bit more complicated, because the
domain may include elements which are not members of std::set.
Basically, set<>::find first locates an element i such that for
all previous elements j, j < x, and i for i, !(i < x) (which
implies that the same holds for all following elements, of
course). Having found this i, if !(x < i) determines whether
the elements are equivalent or not: if !(i < x) && !(x < i), the
elements are considered to be equivalent.

Note the "considered to be". You can easily define an operator<
which is inconsistent with ==. (He has, in fact: his ltstr is
inconsistent with == over char const*. Of course, if he were
also interested in ==, he'd define an eqstr, which returned
strcmp(s1, s2) == 0.)

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Thanks James. It was a really nice explanation, I was linking my ltstr
operator with the find function. find has it's own searching algorithm
as you mentioned, no matter what operator I provide. My operator is
used for arranging the tree while insertion.
There is no meaning providing eqstr operator for set, it defies it's
purpose. I won't be able to add any other element (which differs from
first) to the set except the first one.
Jun 27 '08 #13
"S S" <sa***********@gmail.comwrote in message
news:7c**********************************@a23g2000 hsc.googlegroups.com...
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
Associative containers never use equality operators when they search -- they
always use < (or its equivalent, whatever its name might be).

The idea is that if a < b and b < a are both false, a and b are presumed to
be equal.
Jun 27 '08 #14

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

Similar topics

5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
1
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass =...
11
by: Paul Emmons | last post by:
In writing a function similar to strstr(), I'm calling both of the arguments "const char *". My compiler (gcc) complains "warning: return discards qualifiers from pointer target type" unless I...
2
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 ...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
10
by: dwaach | last post by:
Hi, I am trying to compile the following program, #include <iostream> using namespace std; typedef char* CHAR; typedef const CHAR CCHAR;
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.