fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <tuple>
  5.  
  6. typedef std::vector<std::vector < int>> Type ;
  7. struct Chrom // to demonstrate
  8. {
  9. Type bit;
  10. };
  11.  
  12. std::tuple<int, int, int> findPosition(const std::vector<Chrom>& vec3D, const int& val)
  13. {
  14. int First = 0, Second = 0, Third = -1; // initilize the positions
  15. for(const Chrom& each_chrom: vec3D)
  16. {
  17. for(const std::vector<int>& innerVec: each_chrom.bit)
  18. {
  19. std::vector <int>::const_iterator get_pos;
  20. get_pos = std::find(innerVec.cbegin(), innerVec.cend(), val);
  21. Third = (*get_pos == val) ? get_pos - innerVec.cbegin(): -1; // check val found otherwise -1
  22. if(Third != -1) return std::make_tuple(First, Second, Third); // if found return them
  23. ++Second;
  24. }
  25. Second = 0;
  26. Third = -1;
  27. ++First;
  28. }
  29. return std::make_tuple(First, Second, Third);
  30. }
  31.  
  32. int main()
  33. {
  34. // this is a 3 dimensional vector
  35. std::vector<Chrom> popcurrent(2); // position inside the popcurrent
  36. popcurrent[0].bit = { {3,2,1}, // (0,0,0) (0,0,1) (0,0,2)
  37. {3,10,1} }; // (0,1,0) (0,1,1) (0,1,2)
  38. popcurrent[1].bit = { {5,8,11}, // (1,0,0) (1,0,1) (1,0,2)
  39. {4,7,1} }; // (1,1,0) (1,1,1) (1,1,2)
  40.  
  41. int pos_popcurrent, pos_bit, pos_inner_vec;
  42. for(int val = 1; val <= 12; ++val)
  43. {
  44. std::cout << "\nCurrently looking for: " << val ;
  45. std::tie(pos_popcurrent, pos_bit, pos_inner_vec) = findPosition(popcurrent, val);
  46. (pos_inner_vec != -1) ?
  47. std::cout << " found @ popcurrent[ " << pos_popcurrent << " ].bit[ " << pos_bit << " ][ " << pos_inner_vec <<" ]":
  48. std::cout << " Not found";
  49. }
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
Currently looking for: 1  found @ popcurrent[ 0 ].bit[ 0 ][ 2 ]
Currently looking for: 2  found @ popcurrent[ 0 ].bit[ 0 ][ 1 ]
Currently looking for: 3  found @ popcurrent[ 0 ].bit[ 0 ][ 0 ]
Currently looking for: 4  found @ popcurrent[ 1 ].bit[ 1 ][ 0 ]
Currently looking for: 5  found @ popcurrent[ 1 ].bit[ 0 ][ 0 ]
Currently looking for: 6  Not found
Currently looking for: 7  found @ popcurrent[ 1 ].bit[ 1 ][ 1 ]
Currently looking for: 8  found @ popcurrent[ 1 ].bit[ 0 ][ 1 ]
Currently looking for: 9  Not found
Currently looking for: 10  found @ popcurrent[ 0 ].bit[ 1 ][ 1 ]
Currently looking for: 11  found @ popcurrent[ 1 ].bit[ 0 ][ 2 ]
Currently looking for: 12  Not found