STL set的用法总结

        工作上遇到一些set的使用问题,上网查了一下set的用法,大多数被称为“STL set学习笔记”的文章,几乎都照搬C++ primer上的介绍。此书一直在手边,作为一本工具书,却总是不能解决我太多的问题,除了简单的概念问题。。。

        终于找到一篇,基本算是能解决我的问题。根据自己的情况,写了一些代码验证,总结如下:

1. set容器是有序的
    因此,向容器中插入数据时(insert),set容器会根据 operator<() 操作符进行排序。
    对于C++内置类型排序是没有问题的,但对于自定义的数据类型(struct or class)set容器就无法自动排序了,需要重载一下 operator<。
2. Linux OS中,stl_set.h里定义了set 容器的原型:
    template, class _Alloc = std::allocator<_Key> > class set;
    在默认情况下,set容器使用less进行比较。
3. 排序满足以下准则:
  •  非对称,若A
  •  可传递,若A
  •  A
    set中判断元素是否相等:
    if(!(A
4. 实现代码如下:
    在Linux环境下通过编译和测试: g++ -m32 -g -O3 -o StlSet main.cpp IpAddressNodePayload.cpp IpAddressNodePayload.h
//IpAddressNodePayload.h

#ifndef __IpAddressNodePayload_H__
#define __IpAddressNodePayload_H__

class IpAddressNodePayload{
public:
    IpAddressNodePayload();
    IpAddressNodePayload(unsigned int ipLow = 0, unsigned int ipHigh = 0, unsigned int link = 0, unsigned int node = 0);
    IpAddressNodePayload(const IpAddressNodePayload& rhs);
    ~IpAddressNodePayload();

    IpAddressNodePayload & operator=(const IpAddressNodePayload & rhs);
    bool operator<(const IpAddressNodePayload& rhs) const;

public:
    unsigned int ipAddrLow;
    unsigned int ipAddrHigh;
    unsigned int linkId;
    unsigned int nodeId;
};

#endif
//IpAddressNodePayload.cc

#include "IpAddressNodePayload.h"

IpAddressNodePayload::IpAddressNodePayload()
:
ipAddrLow(0),
ipAddrHigh(0),
linkId(0),
nodeId(0)
{
}

IpAddressNodePayload::IpAddressNodePayload(unsigned int ipLow, unsigned int ipHigh, unsigned int link, unsigned int node)
:
        ipAddrLow(ipLow),
        ipAddrHigh(ipHigh),
        linkId(link),
        nodeId(node)
{
}

IpAddressNodePayload::IpAddressNodePayload(const IpAddressNodePayload& rhs)
:
ipAddrLow(rhs.ipAddrLow),
ipAddrHigh(rhs.ipAddrHigh),
linkId(rhs.linkId),
nodeId(rhs.nodeId)
{
}

IpAddressNodePayload::~IpAddressNodePayload()
{
}

IpAddressNodePayload & IpAddressNodePayload::operator=(const IpAddressNodePayload & rhs)
{
    if(this == &rhs)
    {
        return *this;
    }

    ipAddrLow = rhs.ipAddrLow;
    ipAddrHigh = rhs.ipAddrHigh;
    linkId = rhs.linkId;
    nodeId = rhs.nodeId;

    return *this;
}

bool IpAddressNodePayload::operator<(const IpAddressNodePayload& rhs) const
{
    // The following condition is very important and has to be correct
    // to identify conflicting entries between two ranges whether they
    // are exactly duplicate entries or there is any overlap between them.
    if(ipAddrHigh < rhs.ipAddrLow)
        return true;
    else if(ipAddrHigh == rhs.ipAddrHigh && ipAddrLow == rhs.ipAddrLow)
    {
        if(linkId < rhs.linkId)
            return true;
    }

    return false;
}
//main.cc

//g++ -m32 -g -O3 -o StlSet main.cpp IpAddressNodePayload.cpp IpAddressNodePayload.h
//This is a test for STL::set operator<

#include 
#include 
#include 
#include "IpAddressNodePayload.h"

using namespace std;

int main()
{
    IpAddressNodePayload payload1(1002, 1002, 200, 2000);
    IpAddressNodePayload payload2(1001, 1001, 200, 1999);
    IpAddressNodePayload payload3(1001, 1001, 100, 1987);
    IpAddressNodePayload payload4(1001, 1001, 100, 1987);

    set payloadSet;
    payloadSet.insert(payload1);
    payloadSet.insert(payload2);
    payloadSet.insert(payload3);
    payloadSet.insert(payload4);

    //insert
    set::iterator iter=payloadSet.begin();
    for (;iter!=payloadSet.end();++iter)
    {
        cout<<(*iter).ipAddrLow<<","<<(*iter).ipAddrHigh<<","<<(*iter).linkId<<","<<(*iter).nodeId<::const_iterator setItr = payloadSet.find(payloadKey);
    set::const_iterator setEndItr = payloadSet.end();

    if(setItr != setEndItr)
    {
        cout<

你可能感兴趣的:(学习笔记,C/C++,Linux)