fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <stdlib.h>
  5.  
  6. int main()
  7. {
  8. int comparevalue = 3;
  9. typedef std::vector<int> intvec;
  10. intvec myvec;
  11.  
  12. myvec.push_back(1);
  13. myvec.push_back(2);
  14. myvec.push_back(4);
  15. myvec.push_back(5);
  16. myvec.push_back(6);
  17. myvec.push_back(7);
  18.  
  19. typedef std::map<int, int> intmap;
  20. intmap mymap;
  21.  
  22. for (intvec::const_iterator itr = myvec.begin(); itr != myvec.end(); ++itr)
  23. mymap.insert(std::make_pair(abs(*itr-comparevalue), *itr));
  24.  
  25. std::cout << "difference:" << mymap.begin()->first << "\n";
  26. std::cout << "value:" << mymap.begin()->second;
  27. return 0;
  28. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
difference:1
value:2