关于hash-map里存对象的一个问题

夹心饼干 2009-04-10 11:04:41
void test_hash()
{
test_0 name[5];
for ( int i=0; i<5; ++i)
{
name[i].m = i+10;
g_testxx[i] = name[i];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
test_hash();
stdext::hash_map<int ,test>::iterator it = g_testxx.begin();
for (; it!=g_testxx.end(); ++it)
{
cout<<it->first<<" "<<it->second.m<<endl;
}

return 0;
}
我的疑问是,为什么test_hash()里存入局部的结构体对象,在hash-map里一样可以找得到值?name的作用域不是只在test_hash()里有效的吗?
...全文
445 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
夹心饼干 2009-04-11
  • 打赏
  • 举报
回复
怎么没有人帮忙回答啊?
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
我知道g_testxx是全局的对象,我是说里面存的name[],这个是局部的,为什么还是有效?
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
我知道g_testxx是全局的对象,我是说里面存的name[],这个是局部的,为什么还是有效?
  • 打赏
  • 举报
回复
g_testxx[i] = name[i];


这是全局啊,大哥
mengde007 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stormlk1983 的回复:]
哦不好意思少发了一段代码
C/C++ codetypedefstructtest
{intm;
}test_0;

stdext::hash_map<int,test>g_testxx;usingnamespacestd;voidtest_hash()
{
test_0 name[5];for(inti=0; i<5;++i)
{
name[i].m=i+10;
g_testxx[i]=name[i];
}
}int_tmain(intargc, _TCHAR*argv[])
{
test_hash();
stdext::hash_map<int,test>::iterator it=g_testxx.begin();for(; it!=g_testxx.end();++it)
{…
[/Quote]
g_testxx;
是全局对象,所以能……
mengde007 2009-04-10
  • 打赏
  • 举报
回复
……
  • 打赏
  • 举报
回复
for ( int i=0; i<5; ++i)
{
name[i].m = i+10;
g_testxx[i] = name[i]; //这里不是插入了嘛
}

夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
怎么没人回?自己顶下
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
哦不好意思少发了一段代码
typedef struct test
{
int m;
}test_0;

stdext::hash_map<int ,test> g_testxx;
using namespace std;

void test_hash()
{
test_0 name[5];
for ( int i=0; i<5; ++i)
{
name[i].m = i+10;
g_testxx[i] = name[i];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
test_hash();
stdext::hash_map<int ,test>::iterator it = g_testxx.begin();
for (; it!=g_testxx.end(); ++it)
{
cout<<it->first<<" "<<it->second.m<<endl;
}
return 0;
}
mengde007 2009-04-10
  • 打赏
  • 举报
回复
还是不清楚test_0 name[5];
是怎么回事
liliangbao 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stormlk1983 的回复:]
g_testxx有声明的是全局的,代码是没问题的
[/Quote]
帮顶~
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
g_testxx有声明的是全局的,代码是没问题的
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jimmy_w 的回复:]
引用 12 楼 stormlk1983 的回复:
各位我问的里面的name[]是局部的,为什么在外部g_testxx里还能够找到?原理是什么
难道是加到g_testxx里去了后就自动的给it->second分配了内存空间吗?

拷贝进去了,stl容器都是基于拷贝的
[/Quote]
LS的,拷贝的是值还是地址?
如果是值,那如果我的局部对象里有指针成员该怎么办?
如果是地址,局部对象出了作用域就释放了战内存啊?
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jimmy_w 的回复:]
引用 12 楼 stormlk1983 的回复:
各位我问的里面的name[]是局部的,为什么在外部g_testxx里还能够找到?原理是什么
难道是加到g_testxx里去了后就自动的给it->second分配了内存空间吗?

拷贝进去了,stl容器都是基于拷贝的
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 stormlk1983 的回复:]
各位我问的里面的name[]是局部的,为什么在外部g_testxx里还能够找到?原理是什么
难道是加到g_testxx里去了后就自动的给it->second分配了内存空间吗?
[/Quote]
拷贝进去了,stl容器都是基于拷贝的
夹心饼干 2009-04-10
  • 打赏
  • 举报
回复
各位我问的里面的name[]是局部的,为什么在外部g_testxx里还能够找到?原理是什么
难道是加到g_testxx里去了后就自动的给it->second分配了内存空间吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧