#ifndef keyValuePairHPP #define keyValuePairHPP #include #include #include template class key_value_pair { private: keyT m_key; valueT m_value; public: key_value_pair(keyT theKey, valueT theValue) : m_key(theKey), m_value(theValue) {} keyT getKey() const { return m_key; } valueT getValue() const { return m_value; } void setKey(keyT theKey) { m_key = theKey; } void setValue(valueT theValue) { m_value = theValue; } }; template key_value_pair find_max(std::vector theVector) { if (theVector.size() == 0) return key_value_pair(-1, T()); #if 0 key_value_pair greatest = key_value_pair(0, theVector[0]); for (int index = 1; index < theVector.size(); index++) { if (theVector[index] > greatest.getValue()) greatest = key_value_pair(index, theVector[index]); } return greatest; #else int positionOfGreatest = 0; for (int index = 1; index < theVector.size(); index++) { if (theVector[index] > theVector[positionOfGreatest]) positionOfGreatest = index; } return key_value_pair(positionOfGreatest, theVector[positionOfGreatest]); #endif } void test() { std::vector vec = { 1, 8, 3, 2, 4 }; key_value_pair max = find_max(vec); std::cout << "Position: " << max.getKey() << ", Value: " << max.getValue() << "\n"; } #endif