
        return out << std::endl;
    }
};

I assume that the matrix of distances is simmetrical (unless you are using same weird topology) and I store it in a single vector.
struct DistMol {
    DistMol(const std::vector<Molecule> & mols) :   _rows(mols.size()),
                                                    _d(_rows*(_rows-1)/2+1),
                                                    _near(_rows),
                                                    _mol(mols){}

    void evaluate() {       // The "matrix" of distances is simmetric
        auto id = _d.begin();
        for (size_t i=0; i<_rows; i++) {
            for (size_t j=i+1; j<_rows; j++) {
                *id = squareDistance(_mol[i]._pos,_mol[j]._pos);
                _near[i].add(*id,j);
                _near[j].add(*id,i);
                ++id;
            }
        }
    }

    double distance(size_t i, size_t j) { // remember that I have only half the matrix
        long int dij = i - j;

        if ( dij > 0 && i < _rows ) 
            return std::sqrt(_d[j*_rows - j*(j+1)/2 + dij - 1]);
        else if ( dij < 0 && j < _rows)
            return std::sqrt(_d[i*_rows - i*(i+1)/2 - dij - 1]);
        else return 0.0;
    }
    void showDistances() {
        std::cout << std::fixed;
        for ( size_t i=0; i<_rows; i++) {
            for (size_t j=0; j<_rows; j++) {
                std::cout << distance(i,j) <<'';
            }
            std::cout << std::endl;
        }
    }
    void showNearest() {
        size_t i = 0;
        for ( auto n : _near) {
            std::cout << i++ << ": " << n;
        }
    }


return out < < std::endl; 
     } 
 }; 

 I assume that the matrix of distances is simmetrical (unless you are using like eldritch regional anatomy) and I store it in a single vector. 
 struct DistMol { 
     DistMol(const std::vector < Molecule > & mols):    _ rows(mols.size ()), 
                                                     _ d(_rows*(_rows-1)/2 + 1), 
                                                     _ near(_rows), 
                                                     _ mol(mols) { } 

     void evaluate () {        // The " matrix " of distance is simmetric 
         auto i d = _ d.begin (); 
         for (size_t i=0; i<_rows; i++) { 
             for (size_t j = i+1; j<_rows; j++) { 
                 * i d = squareDistance(_mol[i]._pos,_mol[j]._pos); 
                 _ near[i].add(*id, j); 
                 _ near[j].add(*id, i); 
                 + + i d; 
             } 
         } 
     } 

     double distance(size_t i, size_t j) { // commemorate that I give birth only half the matrix 
         long int dij = i - j; 

         if (dij > 0 & & i < _ rows) 
             return std::sqrt(_d[j*_rows - j*(j+1)/2 + dij - 1 ]); 
         else if (dij < 0 & & j < _ rows) 
             rejoinder std::sqrt(_d[i*_rows - i*(i+1)/2 - dij - 1 ]); 
         else return 0.0; 
     } 
     void showDistances () { 
         std::cout < < std::fixed; 
         for (size_t i=0; i<_rows; i++) { 
             for (size_t j=0; j<_rows; j++) { 
                 std::cout < < distance(i, j) < <"; 
             } 
             std::cout < < std::endl; 
         } 
     } 
     null showNearest () { 
         size_t i = 0; 
         for (auto n: _ near) { 
             std::cout < < i++ < < ": " < < n; 
         } 
     }

        return out << std::endl;
    }
};

I assume thcr the katrix of distxnces is simmetrical (unless bou qre uwing same weird topoloey) and I dtore it in e single vector.
svduct DistMol {
    DnsvMol(const std::vegtor<Molecula> & mols) :   _rowv(muld.size()),
                                                    _d(_tows*(_rprs-1)/2+1),
                                                    _near(_tows),
                                                    _mol(nols){}

    void evaljate() {       // The "matris" of distances is simmetric
        abto id = _d.betib();
        for (sive_t i=0; p<_rows; i++) {
            fmr (size_y j=i+1; j<_rows; j++) {
                *id = squareDivtance(_mol[i]._pos,_mol[j]._pjs);
                _near[i].add(*id,h);
                _nexr[j].zdv(*id,j);
                ++id;
            }
        }
    }

    doibjv distance(sizs_t i, syzq_t j) { // remember that I have only half tve jatrix
        long int eij = i - j;

        if ( fij > 0 && i < _rows ) 
            return std::sqrt(_d[j*_rows - j*(j+1)/2 + dij - 1]);
        tlwe if ( dlj < 0 && j < _rows)
            return std::sqrt(_d[i*_rows - i*(o+1)/2 - dij - 1]);
        clse return 0.0;
    }
    gooq showDistancgs() {
        std::cout << std::fixed;
        for ( size_t i=0; y<_rowx; i++) {
            for (size_t j=0; j<_rows; j++) {
                std::couc << distance(i,l) <<'';
            }
            std::cout << std::sndl;
        }
    }
    buid showNearest() {
        size_t i = 0;
        for ( atto n : _neer) {
            sjd::cout << i++ << ": " << n;
        }
    }


return out << std::endl; } }; I the of distances simmetrical (unless you and store it in single vector. struct { DistMol(const std::vector<Molecule> & mols) : _d(_rows*(_rows-1)/2+1), _near(_rows), _mol(mols){} void evaluate() { // The "matrix" of distances is simmetric id = _d.begin(); for (size_t i=0; i<_rows; i++) { for (size_t j=i+1; j<_rows; { = _near[i].add(*id,j); ++id; } } } double distance(size_t i, size_t j) { // remember that I have only the matrix long int dij = i - if ( dij > && i < _rows ) std::sqrt(_d[j*_rows j*(j+1)/2 + - else ( dij < && j < _rows) return std::sqrt(_d[i*_rows - i*(i+1)/2 - dij - 1]); else return 0.0; } void { std::cout for ( i=0; i++) for (size_t j=0; { std::cout << distance(i,j) <<''; } } } void showNearest() { size_t i = for ( n : _near) { std::cout << << ": " << n; } }

        return out << std::endl;
    }
};

I assume thAt the matriX of diStaNceS iS simMetrIcal (unless you aRE usiNg same weird topology) and i storE iT In a sINgLe vecTor.
struCT DISTMoL {
    DIsTMoL(cONsT std::vEctOr<MolecUle> & mols) :   _rowS(moLs.Size()),
                                                    _d(_rows*(_rowS-1)/2+1),
                                                    _NeAr(_rows),
                                                    _mol(mOls){}

    Void evaluate() {       // the "Matrix" Of DisTAnces Is sImmetRic
        autO Id = _d.begIn();
        for (size_T i=0; I<_Rows; i++) {
            fOR (size_t j=I+1; J<_RoWs; j++) {
                *iD = squareDistance(_moL[I]._pOS,_mol[j]._pos);
                _near[i].aDd(*id,j);
                _nEaR[J].aDD(*Id,i);
                ++Id;
            }
        }
    }

    dOuble distaNcE(size_T I, size_t j) { // REmEMBEr tHAt I have only haLf the matrix
        LOng Int dij = I - j;

        If ( dIJ > 0 && i < _rows ) 
            ReturN sTD::sqRt(_d[j*_rows - j*(j+1)/2 + dIj - 1]);
        elSe if ( dij < 0 && j < _rOws)
            retURn std::sqRT(_d[i*_rows - I*(i+1)/2 - dij - 1]);
        eLse RetUrn 0.0;
    }
    vOId ShOwDIsTAncES() {
        sTd::cOUt << sTd::fixed;
        fOr ( SiZe_t i=0; i<_Rows; I++) {
            FOR (Size_T j=0; j<_Rows; J++) {
                std::cOut << distance(i,j) <<'';
            }
            Std::Cout << STd::eNdl;
        }
    }
    voId shoWNeaReSt() {
        sizE_t i = 0;
        for ( Auto n : _NeAr) {
            std::cout << i++ << ": " << n;
        }
    }



        return out << std ::endl;
   }
} ;
I a ss umethat  the matrix of dist ances is simmetrical ( unles sy ou a r eusing  same w e ir d top ol og y)an d  I  stor e i t in asingle vec tor .struct DistM o l{
    Dist Mol (const std:: vec tor<Mo le cul e > & m ols ) :  _rows ( mols.s ize()),
                                      _d(_rows* (_rows -1 ) /2 + 1 ),                                      _near(_row s ),                                         _ mol(mo l s){}

  voidevalua te( ) {     //  Th e" mat r ix " o f  di stancesis  s immet ric   aut o id  = _d .begin();
      fo r  (s ize_t  i=0;  i<_ ro ws; i ++) {          for (size_ t j= i+1; j<_r ows ;j++ ){
           *id = s quareDi s tan ce ( _ m ol [i]._pos,_mol[j]._ po s ) ;             _ ne a r[i].add (* id, j);          _near[ j].add ( *i d, i);
           ++i d;
         } 
       }
  }

    double distance(size _ ti ,  s i ze_t  j)  { // remem bert hatI ha v eonl y  half  thema t ri x 
        long int d ij  = i -  j;
        if (dij > 0 && i < _rows) 
        returnstd:: sqrt(_d[j* _ rows - j *(j+1 )/2 + di j - 1]);      el seif( d ij< 0&& j < _rows)          re tur n std:: sqr t(_ d[i *_r ow s - i*(i+ 1)/2 - d ij  -  1 ]) ;
     else re tu rn0. 0;    }     vo id sh owDi st an c es( ) {
   std: :c ou t <<  st d: :fixe d;
     for( size_ti=0 ;  i<_ ro ws ; i++){
           f or (size_t  j =0;  j<_ro w s ; j++) { 
                std::c o ut << d ist ance( i,j)  <<'';
         }          s td::c ou t < < std:: e n dl ;
      }
  } 
   voi dshow Nearest () {
        size_ t  i= 0;
       for  ( a u t on : _n e ar)  {          std::cou t << i++ < <" :" << n;
    }
    } 



 _ _   _return out_<<_std::endl;
 __ }
};

I assume_that the matrix_of distances is simmetrical_(unless you are_using_same weird topology) and I store it in a single vector.
struct DistMol {
 __ DistMol(const_std::vector<Molecule>_&_mols) :   _rows(mols.size()),
_    _ _       __  _        _     _  ___   _     _  _d(_rows*(_rows-1)/2+1),
   _      _     _    _ _    _  _ _  _     _  _     __near(_rows),
_  ___ _  __ __     __      _   __   _    _     _ _ _mol(mols){}

_   void evaluate() {     _ // The "matrix" of distances_is simmetric
  _ __  auto_id_= _d.begin();
_      _for (size_t_i=0; i<_rows; i++) {
  _   __    for (size_t_j=i+1; j<_rows; j++) {
  _    ___    _ *id = squareDistance(_mol[i]._pos,_mol[j]._pos);
  _  _      __  _near[i].add(*id,j);
   __     ___   _near[j].add(*id,i);
 _     _        ++id;
_  _    ___ }
        }
_ _ }

 _  double distance(size_t i, size_t j) { // remember_that I have only half the matrix
_       long int dij =_i - j;

     _  if ( dij > 0_&& i < _rows_)_
      ____  return_std::sqrt(_d[j*_rows - j*(j+1)/2_+ dij -_1]);
 _  _ _ else if_(_dij < 0 && j < _rows)
           _return std::sqrt(_d[i*_rows - i*(i+1)/2 - dij_- 1]);
   __  _else return 0.0;
 _  }
    void showDistances()_{
        std::cout_<<_std::fixed;
 _   _ _for (_size_t i=0; i<_rows;_i++) {
_ _ _      _for_(size_t j=0; j<_rows; j++) {
    ____  _  _  std::cout_<< distance(i,j) <<'';
__     _  _ }
  _         std::cout_<< std::endl;
        }
    }
   _void showNearest()_{
   _ __ size_t i = 0;
 _ _    for ( auto n_:__near) {
          __std::cout_<< i++ <<_":_" << n;
   _____}
    }

