Q:

Find similar distances between all values in vector and subset them

Given is a vector with double values. I want to know which distances between any elements of this vector have a similar distance to each other. In the best case, the result is a vector of subsets of the original values where subsets should have at least n members. 
//given
vector<double> values = {1,2,3,4,8,10,12}; //with simple values as example

//some algorithm

//desired result as:
vector<vector<double> > subset;
//in case of above example I would expect some result like:
//subset[0] = {1,2,3,4}; //distance 1
//subset[1] = {8,10,12}; //distance 2
//subset[2] = {4,8,12}; // distance 4
//subset[3] = {2,4};    //also distance 2 but not connected with subset[1]
//subset[4] = {1,3};    //also distance 2 but not connected with subset[1] or subset[3]
//many others if n is just 2. If n is 3 (normally the minimum) these small subsets should be excluded.

This example is simplified as the distances of integer numbers could be iterated and tested for the vector which is not the case for double or float.
My idea so far
I thought of something like calculating the distances and storing them in a vector. Creating a difference distance matrix and thresholding this matrix for some tolerance for similar distances.
//Calculate distances: result is a vector
vector<double> distances;
for (int i = 0; i < values.size(); i++)
    for (int j = 0; j < values.size(); j++)
    {
        if (i >= j)
            continue;
        distances.push_back(abs(values[i] - values[j]));
    }
//Calculate difference of these distances: result is a matrix
Mat DiffDistances = Mat::zero(Size(distances.size(), distances.size()), CV_32FC1);
for (int i = 0; i < distances.size(); i++)
    for (int j = 0; j < distances.size(); j++)
    {
        if (i >= j)
            continue;
        DiffDistances.at<float>(i,j) = abs(distances[i], distances[j]);
    }
//threshold this matrix with some tolerance in difference distances
threshold(DiffDistances, DiffDistances, maxDistTol, 255, CV_THRESH_BINARY_INV);
//get points with similar distances
vector<Points> DiffDistancePoints;
findNonZero(DiffDistances, DiffDistancePoints);

At this point I get stuck with finding the original values corresponding to my similar distances. It should be possible to find them, but it seems very complicated to trace back the indices and I wonder if there isn't an easier way to solve the problem.

A:

Here is a solution that works, as long as there are no branches meaning, that there are no values closer together than 2*threshold. That is the valid neighbor region because neighboring bonds should differ by less than the threshold, if I understood @Phann correctly.
The solution is definitively neither the fastest nor the nicest possible solution. But you might use it as a starting point:
#include <iostream>
#include <vector>
#include <algorithm>

int main(){
    std::vector< double > values = {1,2,3,4,8,10,12};
    const unsigned int nValues = values.size();
    std::vector< std::vector< double > > distanceMatrix(nValues - 1);
    // The distanceMatrix has a triangular shape
    // First vector contains all distances to value zero
    // Second row all distances to value one for larger values
    // nth row all distances to value n-1 except those already covered
    std::vector< std::vector< double > > similarDistanceSubsets;
    double threshold = 0.05;

    std::sort(values.begin(), values.end());

    for (unsigned int i = 0; i < nValues-1; ++i) {
        distanceMatrix.at(i).resize(nValues-i-1);
        for (unsigned j = i+1; j < nValues; ++j){
            distanceMatrix.at(i).at(j-i-1) = values.at(j) - values.at(i);
        }
    }

    for (unsigned int i = 0; i < nValues-1; ++i) {
        for (unsigned int j = i+1; j < nValues; ++j) {
            std::vector< double > thisSubset;
            double thisDist = distanceMatrix.at(i).at(j-i-1);

            // This distance already belongs to another cluster
            if (thisDist < 0) continue;

            double minDist  = thisDist - threshold;
            double maxDist  = thisDist + threshold;
            thisSubset.push_back(values.at(i));
            thisSubset.push_back(values.at(j));
            //Indicate that this is already clustered
            distanceMatrix.at(i).at(j-i-1) = -1;

            unsigned int lastIndex = j;
            for (unsigned int k = j+1; k < nValues; ++k) {
                thisDist = distanceMatrix.at(lastIndex).at(k-lastIndex-1);

                // This distance already belongs to another cluster
                if (thisDist < 0) continue;

                // Check if you found a new valid pair
                if ((thisDist > minDist) && (thisDist < maxDist)){
                    // Update the valid distance interval
                    minDist = thisDist - threshold;
                    minDist = thisDist - threshold;
                    // Add the newly found point
                    thisSubset.push_back(values.at(k));
                    // Indicate that this is already clustered
                    distanceMatrix.at(lastIndex).at(k-lastIndex-1) = -1;
                    // Continue the search from here 
                    lastIndex = k;
                }
            }
            if (thisSubset.size() > 2) {
                similarDistanceSubsets.push_back(thisSubset);
            }
        }
    }
    for (unsigned int i = 0; i < similarDistanceSubsets.size(); ++i) {
        for (unsigned int j = 0; j < similarDistanceSubsets.at(i).size(); ++j) {
            std::cout << similarDistanceSubsets.at(i).at(j);
            if (j != similarDistanceSubsets.at(i).size()-1) {
                std::cout << " ";
            }
            else {
                std::cout << std::endl;
            }
        }
    }
}

The idea is to precompute the distances and then look for every pair of particles, starting from the smallest and its larger neighbors, if there is another valid pair above it. If so these are all collected in a subset and this is added to the subset vector. For every new value the valid neighbor region has to be updated to ensure that neighboring distances differ by less than the threshold. Afterwards, the program continues with the next smallest value and its larger neighbors and so on.

