mardi 9 juillet 2019

How can I do make_pair within a template class method using unordered_multimap?

I have the following code, I know I am almost reimplementing the container, but I want to do like this, I will have more specific methods and I don't want to repeat code for multimaps of different types:

template<class TYPE>
class MapTemplate {
public:
    typedef typename std::unordered_multimap <QString, QString>::iterator iterator;

void addElement(const QString& elementName, const TYPE& T) {
    unorderedMultiMap.insert(std::make_pair<QString, TYPE>(elementName, T));
}
std::pair<iterator, iterator> getEqual_range(const QString& elementName) {
    return unorderedMultiMap.equal_range(elementName);
}
int removeElement(const QString& elementName) {
    return unorderedMultiMap.erase(elementName);
}
int getNumberOfElements() const {
    return unorderedMultiMap.size();
}
bool isMapEmpty() const {
    return unorderedMultiMap.isEmpty();
}
iterator isElementInMap(const QString& elementName) const {
    return unorderedMultiMap.find(elementName);
}

private:
    std::unordered_multimap<QString, TYPE> unorderedMultiMap;
};

I have two questions related: If I try userProgMap.addElement(userName, programName); being MapTemplate<QString> userProgMap and userName & programName both QString, I get an error saying cannot convert argument from QString to _Ty1&&.

On the other hand, how could I have an iterator like that:

typedef typename std::unordered_multimap <QString, TYPE>::iterator iterator;

Would it be possible?. That also shows an error, I must use <QString, QString>

Thank you.

Aucun commentaire:

Enregistrer un commentaire