【发布时间】:2012-10-19 06:28:14
【问题描述】:
我正在使用 stl map 来存储从 pcap 文件中提取的流信息。当数据包到来时,我使用 map.find 来查找数据包所属的流是否存在。我必须使用 map.find 两次,因为从 A 到 B 的数据包和从 B 到 A 的数据包属于同一流。
struct FiveTuple
{
unsigned short source_port;
unsigned short dest_port;
unsigned int source_ip_addr;
unsigned int dest_ip_addr;
unsigned char transport_proto_type;
};
FiveTuple 标识一个流。我使用 FiveTuple 作为 map 中的关键元素。
map 是 map 在vs2010的debug版本中,结果是合理的。当我将其更改为发布版本时,我发现第二个 m.find 大部分时间都没有返回正确的迭代器,而是给了我 m.end。而且我发现没有初始化问题。发布版本问题如何解决?auto it = m.find(five_tuple);
if( it == m.end())
{
//swap source and dest ip/port in five_tuple,
it = m.find(five_tuple);
if(it == m.end())
{
//do something
}
}
【问题讨论】:
-
尝试将
operator<添加到您的元组结构中。只是猜测。 -
@Naveen:没用,因为他将明确的比较器传递给 map。
-
@Yihong Xiang:Debug 构建将对象/内置/等的内存初始化为
0是典型的(也是愚蠢的......),这保证了稳定性。但是在 Release 中,这种“有用”的 0-ing 被删除了,因为它通常是不必要的......
标签: c++ dictionary stl