c++ map中key为结构体

map中key为结构体

项目中需要将结构体作为hash的key,一开始用hash_map,但是结构体中多值比较一直失败,所以尝试了map。

hash_map 查找速度会比map快,而且查找速度基本和数据量大小无关,属于常数级别;而map的查找速度是log(n)级别。hash还有hash函数的耗时。当有100w条记录的时候,map也只需要20次的比较,200w也只需要21次的比较!所以并不一定常数就比log(n) 小。

hash_map对空间的要求要比map高很多,所以是以空间换时间的方法,而且,hash_map如果hash函数和hash因子选择不好的话,也许不会达到你要的效果,

typedef struct ip_tuple
{
    int src_port;
    int dst_port;
    char* src_ip;
 }IPTUPLE;

但是编译的时候,报了这个错误
d:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: ‘bool __cdecl std::operator <(const class std::multimap<_K,_Ty,_Pr,_A> &,const class std::multimap<_K,_Ty,_Pr,_A> &)’ : could not deduce template argument for ‘const
class std::multimap<_K,_Ty,_Pr,_A> &’ from ‘const struct ip_tuple’
问题肯定出在这个key上面,后来百度了下,才知道原因,原来map中的key默认是以less<>升序对元素排序(排序准则也可以修改),也就是说key必须具备operator<对元素排序,也就是要重载<运算符,而平常我们的用的基本上都是基本类型元素作为key,所以就不存在这个问题了。

改正后的结构体如下:

#include <string>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
 
 
typedef struct ip_tuple
{
    int src_port;
    int dst_port;
    char* src_ip;
 
    bool operator <(const ip_tuple& other) const
    {
        if (src_port < other.src_port)        //src_port按升序排序
        {
            return true;
        }
        else if (src_port == other.src_port)  //src_port相同,按dst_port升序排序
        {
            if(dst_port < other.dst_port)
            {
                return true;
            }
            else if(dst_port == other.dst_port)//dst_port,src_port相同,比较src_ip
            {
                if(strcmp(src_ip,other.src_ip)!=0)
                {
                    return true;
                }
            }
 
        }
        return false;
    }
} IPTUPLE;
 
int main()
{
    map<IPTUPLE, int>m_roadMap;
    for(int i=1; i<6; i++)
    {
        IPTUPLE it;
        it.src_port = i;
        it.dst_port = i*10;
        if(i==1)
        it.src_ip="aaa";
        if(i==2)
        it.src_ip="bbb";
        if(i==3)
        it.src_ip="ccc";
        if(i==4)
        it.src_ip="ddd";
        if(i==5)
        it.src_ip="bbb";
        m_roadMap.insert(make_pair(it, i*200));
    }
//==========
    IPTUPLE ite;
    ite.src_port = 2;
    ite.dst_port = 20;
    ite.src_ip="bbb";
    map<IPTUPLE, int>::iterator itor;
 
    itor = m_roadMap.find(ite);
 
    if (itor != m_roadMap.end())
    {
        cout<<itor->second;
    }
 
}
C++,如果你想要通过`std::map`存储的结构体里面的某个时间字段进行排序,首先需要明确`std::map`默认是根据键(key)来排序的,其内部实现是一个红黑树。如果想要根据值(value)的时间字段进行排序,你需要使用其他数据结构。 一种常见的方法是使用`std::vector`来存储结构体实例,并使用`std::sort`函数来根据时间字段对这个向量进行排序。下面是一个简单的示例代码: ```cpp #include <vector> #include <algorithm> // for std::sort #include <ctime> // for time_t and related functions // 假设有一个结构体定义如下: struct MyStruct { // ... 可能有其他成员变量 ... time_t timeField; // 存储时间的时间戳 }; // 比较函数,用于根据时间字段对结构体实例进行排序 bool compareByTime(const MyStruct& a, const MyStruct& b) { return a.timeField < b.timeField; } // ... 在你的代码 ... // 假设我们有一个包含多个MyStruct实例的vector std::vector<MyStruct> structs; // ... 添加元素到vector ... // 使用std::sort对结构体数组进行排序 std::sort(structs.begin(), structs.end(), compareByTime); // 此时,structs已经按照时间字段升序排列 ``` 在上面的代码,`MyStruct`是包含时间字段`timeField`的结构体,`compareByTime`是一个比较函数,它将作为`std::sort`的第三个参数。在调用`std::sort`之后,`structs`向量的元素将根据时间字段按升序排列。 如果结构体很大或者有复杂的内存布局,更高效的做法可能是实现一个自定义的比较类或者重载`<`运算符,这样可以避免函数调用的开销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值