How to Insert your Own Custom Objects Inside a std:::set

So, now if you need to push a bunch of custom objects (objects of your own class), how can you design your class so that they can be contained in set/multiset.

As I said earlier: "The elements of set/multiset can be of any type T, that is:

Assignable (that is you have to have an overloaded assignment operator, or default assignment operator - if it is sufficient enough to do the job)
Copyable (that is you have to have an copy constructor, or default copy constructor - if it is sufficient enough to do the job) 
Comparable: This is the most important part. You have to put some kind of decision making branch which will compare two custom defined objects based on some comparison criteria. Here you have two choices:
You can overload a "<" operator inside your class itself.
You can create a functor to do the comparison job.
Another important thing is, the sorting criterion must define "String Weak Ordering". Strict Weak Ordering is defined by following three properties:

It has to be anti-symmetric, aka if x<y is true then y<x is false.
It has to be transitive: aka, if x<y is true and y<z is true then x<z is also true.
It has to be irreflexive: aka x<x is always false.
Using the Code
If you just unzip the source, you'll find three source files named:MyType.hpp - This class defines the custom defined type along with overloaded < operator. 
MyComparator.hpp - This class defines a custom define functor which does the comparison job.
newmain.cpp - The main file.
First let us analyze the MyType.hpp file:

/* 
 * File:   MyType.hpp
 * Author: Tushar
 *
 * Created on May 6, 2009, 5:10 PM
 */

#ifndef _MYTYPE_HPP
#define    _MYTYPE_HPP

namespace Tushar{
template &lt;typename T>
class MyType{
private:
    T value;
public:
    MyType(){
        value = T();
    }
    MyType(T val):value(val){
    }
    MyType(const MyType& other){
        value = other.value;
    }
    MyType& operator=(MyType const& other){
        if((void*)this == (void*)&other)
            return *this;
        value = other.getValue();
        return *this;
    }

    bool operator&lt; (const MyType& other) const{
        return value&lt;other.getValue();
    }
    T getValue() const{
        return value;
    }
};
}

#endif    /* _MYTYPE_HPP */

Here, everything is very much simple. Just go through it.

Now, let's have a look at MyComparator.hpp.

/* 
 * File:   MyComparator.hpp
 * Author: Tushar
 *
 * Created on May 6, 2009, 5:11 PM
 */

#ifndef _MYCOMPARATOR_HPP
#define    _MYCOMPARATOR_HPP
#include "MyType.hpp"
template &lt;typename T>
class MyComparator{
public:
    bool operator() (const T& t1, const T& t2) const{
        return t1.getValue()&lt;t2.getValue();
    }
};
#endif    /* _MYCOMPARATOR_HPP */


Here the idea is to overload the () operator (it's a true functor) which takes two templated args (we'll pass two custom defined objects actually), it compares the two objects by using the < operator.Now let's see the driver file:

#include <iostream>
#include <set>
#include "myType.hpp"
#include "MyComparator.hpp"

int main()
{  
    Tushar::MyType&lt;int> m1(100);
    Tushar::MyType&lt;int> m2(200);       
    typedef std::set&lt;Tushar::MyType&lt;int>, MyComparator&lt;Tushar::MyType&lt;int> > > CustomSet;
    CustomSet mySet;
    mySet.insert(m1);
    mySet.insert(m2);
    CustomSet::const_iterator it(mySet.begin());
    for(;it!=mySet.end();++it){
        std::cout&lt;&lt;"Element is: "&lt;&lt;it->getValue()&lt;&lt;std::endl;
    }
    return 0;
}


 

转载于:https://my.oschina.net/wdyoschina/blog/1503002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值