STL set / map w.r.t const char * [英] STL set/map w.r.t const char*

查看:40
本文介绍了STL set / map w.r.t const char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个非常基本的问题,但这是一个很好的问题。下面是代码

片段。


struct ltstr

{

bool operator()( const char * s1,const char * s2)const

{

返回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<< 找到;

其他

cout<< 未找到;

返回0;

}


输出将为 - 找到

我的问题是,为什么?


如何将const char *与另一个const char *进行比较

找到该值?我没有指定任何相等运算符?我只是提到了strcmp(s1,s2)< b
0

这意味着,当strcmp(s1,s2)== 0(如果匹配)

它将返回false。那么set / map如何能够找到const char *

值?

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?

推荐答案

SS写道:
S S wrote:




我有一个非常基本的问题,但它是一个很好的问题。下面是代码

片段。


struct ltstr

{

bool operator()( const char * s1,const char * s2)const

{

返回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<< 找到;

其他

cout<< 未找到;

返回0;

}


输出将为 - 找到

我的问题是,为什么?


如何将const char *与另一个const char *进行比较

找到该值?我没有指定任何相等运算符?我只是提到了strcmp(s1,s2)< b
0

这意味着,当strcmp(s1,s2)== 0(如果匹配)

它将返回false。那么set / map如何能够找到const char *

的值?
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?



我相信它是未定义的行为。你有一个恒定的短暂的在

你的程序两次。一旦为char数组,一次为

的参数找到。编译器可能看到它们完全相同,所以相反

制作2它只是制作一个并使用相同的指针。


一个例子这是我的系统输出的这个程序:

00416808 00416808

一次我运行它

#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";

}


编译器无需为Hello创建两个常量数组,因为

它是常量它只创建一次并使用相同的地址同时为foo

和bar。


-

Jim Langston
ta*******@rocketmail.com


Jim Langston写道:
Jim Langston wrote:

SS写道:
S S wrote:

>嗨

我有一个非常基本的问题,但这是一个很好的问题。下面是代码片段。

struct ltstr
{bool operator()(const char * s1,const char * s2)const
{
返回strcmp(s1,s2)< 0;
}
};

int main()
{
const int N = 6;
const char * a [N ] = {" isomer,ephemeral,prosaic,
nugatory,artichoke,serif};
设置< const char *,ltstrA( a,a + N);
if(A.find(ephemeral)!= A.end())
cout<< 找到;
其他
cout<< 未找到;
返回0;
}

输出将是 - 找到
我的问题是,为什么?
这意味着,当strcmp(s1,s2)== 0(如果匹配)
它将返回false。那么set / map如何能够找到const char *
值呢?
>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?



我相信它是未定义的行为。你有一个恒定的短暂的在

你的程序两次。一旦为char数组,一次为

的参数找到。编译器可能看到它们完全相同,所以相反

制作2它只是制作一个并使用相同的指针。


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.



无所谓,请注意使用第二个模板参数

std :: set。


-

Ian Collins。

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

--
Ian Collins.


SS写道:
S S wrote:




我有一个非常基本的问题,但这是一个很好的问题。下面是代码

片段。


struct ltstr

{

bool operator()( const char * s1,const char * s2)const

{

返回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<< 找到;

其他

cout<< 未找到;

返回0;

}


输出将为 - 找到

我的问题是,为什么?


如何将const char *与另一个const char *进行比较

找到该值?我没有指定任何相等运算符?我只是提到了strcmp(s1,s2)< b
0

这意味着,当strcmp(s1,s2)== 0(如果匹配)

它将返回false。那么set / map如何能够找到const char *

的值?
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< C和搜索项目X,


如果(X

-

Ian Collins。

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.


这篇关于STL set / map w.r.t const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆