A program using class template, pair, vector

I'm trying to program the following: A template class map having a pointer to a vector that contains elements std::pair<T,Q>, where T and Q are template types. It's supposed to work similarly to std::map and T is 'key' type, whereas Q stands for 'value' type. Besides the following should be implemented: 1. Constructor & destructor. 2. Function empty returning bool (if the object is empty). 3. Function size (using count_if) 4. Function clear that deletes all vector records. 5. Operator [] which allows for: map["PI_value"] = 3.14; It should use function find 6. Operators =, ==, !=, >> (using equal function)

I've been trying to code the above task, but have stuck on the code below. Do you have any ideas to repair this mess?

#include <iostream>
#include <tuple>
#include <utility>
#include <algorithm>
#include <vector>


using namespace std;

template <typename T, typename Q>
class mapa
{
private:
    vector<std::pair<T,Q>>* ptr;
    public:
    /**< DEFAULT CONSTRUCTOR/////////////////////////// */
    mapa()
    {
        ptr = new vector<std::pair<T,Q>>;
        ptr->push_back(std::pair<T,Q>(0,0));
    }
    /**< DESTRUCTOR////////////////////////////////////// */
    ~mapa(){ delete ptr;}
    /**< EMPTY()////////////////////////////// */
    bool empty()
    {
        if(ptr)
            return false;
        else
            return true;
    }
    /**< SIZE()///////////////////////////////// */
    int size()
    {
        return ptr->size();
    }
    /**< CLEAR()///////////////////////////////// */
    void clear()
    {
        ptr->clear(ptr->begin(), ptr->end());
    }
    /**< OPERATOR[]/////////////////////////////////////////// */
    vector<std::pair<T,Q>>* & operator[](T key)
        {
        auto ptr2 = ptr;
        if(empty())
        {
            std::pair<T,Q> para;

            para.first = key;
            para.second = 0;
          ptr2->push_back(para);
          //ptr2->push_back(std::pair<T,Q>(key,0));
        }
        else
        {

            auto ptr2 = find_if( ptr->begin(), ptr->end(),
            [](std::pair<T,Q> example,T key)
                            {
                               return(example.first==key);
                            }
                            );
        }
        return ptr2;
    }
}; //class end

Answers


The lambda provided to std::find_if is declared wrong.

If you see e.g. this reference for std::find_if, you will see that the functions should be like

bool pred(const Type &a)

That means the lambda should be something like

[&key](const std:pair<T, Q>& element) { return element.first == key }

There are also other problems with your operator[] function, like that it should return Q& instead of a reference to the vector pointer. You should also remember that std::find_if returns an iterator to the found element, or end() if not found.


Need Your Help

Boost.lockfree performance on RHEL 5.3

c++ linux boost lock-free rhel5

I'm trying to use the Boost::lockfree::spsc_queue for an application that will be supported on multiple platforms. I tried the spsc_queue.cpp example from boost_1_55_0/libs/examples/lockfree on Win...