Find Triangle Triplets from a list of given numbers If a triplet of segments A, B and C are triangle triplets if and only
if

A + B &gt; C
A + C &gt; B
B + C &gt; A


Is there a better implementation for this problem?
#include&lt;vector&gt;
#include&lt;iostream&gt;

void findTriangleTriplets( std::vector&lt;int&gt;&amp; vec_input, std::vector&lt;std::vector&lt;int&gt;&gt;&amp; triplets)
{
    int size =  vec_input.size();
    for(int i = 0; i &lt; size; ++i)
    {
        for(int j = i+1; j &lt; size; ++j)
        {
            for(int k = 0; k &lt; size; ++k)
            {
                if( ( ( vec_input.at( i ) + vec_input.at( j ) ) &gt; vec_input.at( k ) ) &amp;&amp;
                    ( ( vec_input.at( i ) + vec_input.at( k ) ) &gt; vec_input.at( j ) ) &amp;&amp;
                    ( ( vec_input.at( j ) + vec_input.at( k ) ) &gt; vec_input.at( i ) ) )
                {               
                    std::vector&lt;int&gt; temp;
                    temp.emplace_back( vec_input.at( i ) );
                    temp.emplace_back( vec_input.at( j ) ); 
                    temp.emplace_back( vec_input.at( k ) );
                    triplets.emplace_back( temp );
                }
            }
        }
    }
} 

int main()
{
    std::vector&lt;std::vector&lt;int&gt;&gt;  triplets;
    std::vector&lt;int&gt; vec_input { 1, 2, 3, 4, 5, 6 };
   
    findTriangleTriplets( vec_input,  triplets);
    for ( auto &amp;x : triplets)
    {
        std::cout&lt;&lt;x.at(0)&lt;&lt;&quot; &quot;&lt;&lt;x.at(1)&lt;&lt;&quot; &quot;&lt;&lt;x.at(2)&lt;&lt;&quot;\n&quot;;
    }         

    return 0;
}
First thing to comment on after copying and pasting into my IDE, the following line gives a warning:


int size = vec_input.size();

  
  Implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int'


Apparently, the size() method of Vector returns an unsigned long, and we should use this type for our size, rather than int.

unsigned long size = input.size();




Functions that return void but accept (and modify) an argument passed by reference are always a warning sign for me.  Here, I don't think there's a particularly good reason to do so.

Here, I can't see any good reason to be doing so.  We create and return a vector within the function, and only accept the input argument.



Another problem I see: the result we're giving the caller is a vector of vectors, and the inner vectors are supposed to always have exactly 3 values in it.  Why don't we create a struct to represent the triplets?

struct Triplet {
    int a;
    int b;
    int c;
}


Now we can return a vector of these.



I would also abstract that complex conditional into its own function.

bool isTriplet(int a, int b, int c) {
    return ((a + b) &gt; c) &amp;&amp; ((a + c) &gt; b) &amp;&amp; ((b + c) &gt; a);
}




Now, putting all that together, without changing the algorithm or any of the logic, the code looks like this:

#include&lt;vector&gt;
#include&lt;iostream&gt;

struct Triplet {
    int a;
    int b;
    int c;
};

bool isTriplet(int a, int b, int c) {
    return ((a + b) &gt; c) &amp;&amp; ((a + c) &gt; b) &amp;&amp; ((b + c) &gt; a);
}

std::vector&lt;Triplet&gt; findTriangleTriplets(std::vector&lt;int&gt; input) {
    std::vector&lt;Triplet&gt; triplets;
    unsigned long size = input.size();

    for(int i=0;i&lt;size;++i) {
        for(int j=i+1;j&lt;size;++j) {
            for(int k=0;k&lt;size;++k) {
                int a = input.at(i);
                int b = input.at(j);
                int c = input.at(k);

                if(isTriplet(a, b, c)) {
                    Triplet t = Triplet{a,b,c};
                    triplets.emplace_back(t);
                }
            }
        }
    }

    return triplets;
}

int main()
{
    std::vector&lt;Triplet&gt; triplets;
    std::vector&lt;int&gt; vec_input { 1, 2, 3, 4, 5, 6 };

    triplets = findTriangleTriplets(vec_input);
    for ( auto &amp;x : triplets)
    {
        std::cout&lt;&lt;x.a&lt;&lt;" "&lt;&lt;x.b&lt;&lt;" "&lt;&lt;x.c&lt;&lt;"\n";
    }

    return 0;
}