地图&迭代器 [英] map & iterator

查看:72
本文介绍了地图&迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有奇怪的问题,我有地图< myclass1 *,myclass2 *,比较> ;.

代码:

std: :地图< MyClass1的*,myclass2 *,比较和GT; m = mymap;

std :: map< myclass1 *,myclass2 *,Compare> :: const_itera tor ci;


printf(" Elements:% d \ n",m.size());

for(ci = m.begin(); ci!= m.end(); ci ++){

printf(" text\\\
);

}


问题是我收到:

元素: 5

text

text


所以我在地图中添加了5个元素,但迭代器只通过
进行迭代
其中两个。哪里可能是问题?


Thanx

Michal

解决方案

vertigo写道:

你好
我有奇怪的问题,我有地图< myclass1 *,myclass2 *,比较>。
代码:
std ::地图< MyClass1的*,myclass2 *,比较和GT; m = mymap;
std :: map< myclass1 *,myclass2 *,Compare> :: const_itera tor ci;

printf(" Elements:%d \ n",m。 (ci = m.begin(); ci!= m.end(); ci ++){
printf(" text\\\
);
}

问题是我收到了:
元素:5
文字

所以我在地图上添加了5个元素,但迭代器只通过其中两个迭代。哪里可能是问题?




也许你的比较坏了吗?它是做什么的?


Yevgen




眩晕 <斧*** @ wp.pl>在留言中写道

news:ci ********** @ nemesis.news.tpi.pl ...

你好
我有奇怪的问题,我有地图< myclass1 *,myclass2 *,比较>。
代码:
std :: map< myclass1 *,myclass2 *,Compare> m = mymap;
std :: map< myclass1 *,myclass2 *,Compare> :: const_itera tor ci;

printf(" Elements:%d \ n",m。 (ci = m.begin(); ci!= m.end(); ci ++){
printf(" text\\\
);
}

问题是我收到了:
元素:5
文字

所以我在地图上添加了5个元素,但迭代器只通过其中两个迭代。哪里可能是问题?

Thanx
Michal




这是一个奇怪的问题。解决这些问题的方法是在这里发布一个

完整的程序。然后,数十名愿意参加的志愿者将编译并测试你的代码并给你答案。


John

>也许你的比较坏了?它做了什么?


是的。当我声明/使用没有比较的地图时,一切正常。

我的比较:

struct比较{

bool operator()(sha1 * sha1 ,sha1 * sha2)const {

char * s1;

char * s2;

s1 = sha1_sprintf_hex(sha1);

s2 = sha1_sprintf_hex(sha2);

if(strncmp(s1,s2,40)== 0){

返回false;

}

返回true;

}

};


其中sha1对象代表160位SHA1值,sha1_sprintf_hex(sha)

返回指向char表的指针(很好的格式化SHA1字符串)。


这可能有什么问题?


Thanx

Michal


Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?

Thanx
Michal

解决方案

vertigo wrote:

Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?



Maybe your Compare is broken? What does it do?

Yevgen



"vertigo" <ax***@wp.pl> wrote in message
news:ci**********@nemesis.news.tpi.pl...

Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?

Thanx
Michal



That is a weird problem. The way to solve these problems is to post a
complete program here. Then dozens of willing volunteers will compile and
test your code and give you the answer pronto.

John


> Maybe your Compare is broken? What does it do?

YES. When i declare/use map without Compare everything works fine.
My compare:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
if (strncmp(s1,s2,40)==0){
return false;
}
return true;
}
};

Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
returns pointer to char table (nice formatted SHA1 string).

What could be wrong with that ?

Thanx
Michal


这篇关于地图&amp;迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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