#include <map> #include <iostream> using namespace std; class TwoSum { public: void store(int input) { if (hashtable.find(input) == hashtable.end()) { hashtable[input] = input; } } bool test(int val) { map<int, int>::iterator iter; for (iter = hashtable.begin(); iter != hashtable.end(); iter++) { int cur = iter->second; int left = val - cur; if (hashtable.find(left) != hashtable.end()) return true; } return false; } private: map<int, int> hashtable; }; int main () { TwoSum t; t.store(1); t.store(-2); t.store(3); t.store(6); cout << " sum 4 = "<<t.test(4)<<endl; cout << " sum -1 = "<<t.test(-1)<<endl; cout << " sum 9 = "<<t.test(9)<<endl; cout << " sum 10 = "<<t.test(10)<<endl; cout << " sum 5 = "<<t.test(5)<<endl; cout << " sum 0 = "<<t.test(0)<<endl; return 1; }