插入一序列的key-value的map

来源:互联网 发布:php cookie 跨域共享 编辑:程序博客网 时间:2024/05/22 01:35

MapAssign is used to insert a series of key-value pair into a map. it will help to statically initialize a constant map.

template <typename K, typename V>
class MapAssign
{
public:
    typedef std::map<K, V>   Map;
    typedef std::pair<K, V>  Pair;
    typedef std::deque<Pair> Deque;

    MapAssign(void)
    try : m_qPairs()
    {
    }
    catch (...)
    {
        throw std::exception();
    }

    ~MapAssign(void)
    {
    }

    /// \brief overloading of operator ().
    ///        it enables you to use MapAssign in this
    ///        style:
    ///        MapAssign<K, V>()(key1, value1)(key2, value2);
    ///
    /// \param[in] kKey   key of a single map element.
    /// \param[in] kValue value of a single map element.
    ///
    /// \return reference of current MapAssign object.
    MapAssign& operator()(const K &kKey, const V &kValue)
    {
        try
        {
            m_qPairs.push_back(Pair(kKey, kValue));

            return *this;
        }
        catch (...)
        {
            throw std::exception();
        }
    }

    /// \brief overloading of operator std::map<K, V>
    ///
    /// \return an object of std::map<K, V>.
    operator Map() const
    {
        try
        {
            return Map(m_qPairs.begin(), m_qPairs.end());
        }
        catch (...)
        {
            throw std::exception();
        }
    }

private:
    DISALLOW_COPY_AND_ASSIGN(MapAssign);

    Deque m_qPairs;
};

0 0
原创粉丝点击