Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Напиши код на C++: Реализация класса «multimap». Самостоятельно реализовать основные…

  1. User

    Напиши код на C++: Реализация класса «multimap». Самостоятельно реализовать основные методы данного класса, не используя стандартный класс. В реализации должны присутствовать такие методы как: find(), count() и оператор индексирования «[]». Напиши демонстративный код в функции main()

  2. Assistant

    #include #include #include

    template <typename Key, typename Value> class multimap { public: using KeyType = Key; using ValueType = Value; using iterator = typename std::vector<std::pair<KeyType, ValueType> >::iterator; using const_iterator = typename std::vector<std::pair<KeyType, ValueType> >::const_iterator;

    multimap() {}
    
    void insert(std::pair<KeyType, ValueType> pair) {
        m_data.push_back(pair);
        std::sort(m_data.begin(), m_data.end(),
                  [](const std::pair<KeyType, ValueType>& a, const std::pair<KeyType, ValueType>& b) {
                      return a.first < b.first;
                  });
    }
    
    iterator find(const KeyType& key) {
        auto pos = std::lower_bound(m_data.begin(), m_data.end(), key,
                                    [](const std::pair<KeyType, ValueType>& a, const KeyType& b) {
                                        return a.first < b;
                                    });
        if (pos != m_data.end() && pos->first == key)
            return pos;
        else
            return end();
    }
    
    const_iterator find(const KeyType& key) const {
        auto pos = std::lower_bound(m_data.begin(), m_data.end(), key,
                                    [](const std::pair<KeyType, ValueType>& a, const KeyType& b) {
                                        return a.first < b;
                                    });
        if (pos != m_data.end() && pos->first == key)
            return pos;
        else
            return end();
    }
    
    size_t count(const KeyType& key) const {
        return std::count_if(m_data.begin(), m_data.end(),
                             [&key](const std::pair<KeyType, ValueType>& p) {
                                 return p.first == key;
                             });
    }
    
    ValueType& operator[](const KeyType& key) {
        auto pos = find(key);
        if (pos != end())
            return pos->second;
        else {
            insert({key, ValueType{}});
            return m_data.back().second;
        }
    }
    
    iterator begin() {
        return m_data.begin();
    }
    
    const_iterator begin() const {
        return m_data.begin();
    }
    
    iterator end() {
        return m_data.end();
    }
    
    const_iterator end() const {
        return m_data.end();
    }
    

    private: std::vector<std::pair<KeyType, ValueType>> m_data; };

    int main() { multimap<std::string, int> myMap; myMap.insert({"A", 1}); myMap.insert({"B", 2}); myMap.insert({"A", 3});

    std::cout << "Multimap myMap: " << std::endl;
    for (const auto& p : myMap)
        std::cout << p.first << ", " << p.second << std::endl;
    
    std::cout << "myMap[\"A\"] = " << myMap["A"] << std::endl;
    std::cout << "myMap.count(\"A\") = " << myMap.count("A") << std::endl;
    
    return 0;
    

    }