I do not know, if it is possible to find the minimum first element in a tuple without using sort function. I have a tuple like this : data=[ ( 23, a, 3.78, d), ( 11, x, 5.8, w)] . I need to obtain the second element which is (11, x, 5.8, w) since 11<23. Is there any way to do it without using sort function ?
of course it is. you can iterate all your tuples and keep track of the smallest one via your condition. You don't WANT to sort data to find the lowest element:
iterating each element once and finding the smallest takes N operations.
Sorting, usually, takes at least N(lg(N)) operations, which is only a little more than N for a few items but quite a lot more when you start having many items.
if you only have 2 items, its just a flat compare.
#include <iostream>
#include <tuple>
int main( )
{
auto tup = std::make_tuple( 11, 'x', 5.8, 'w' );
// get specific element within a tuple (zero-based)
std::cout << get<1>( tup ) << '\n';
// tie tuple values to variables, std::ignore used to discard elements
char ch;
std::tie( std::ignore, ch, std::ignore, std::ignore ) = tup;
std::cout << ch << '\n';
// C++17 structured binding
auto [ a, b, c, d ] = tup;
std::cout << b << '\t' << c << '\n';
// if x and w are other variables....
int x { 42 };
double w { 8.65 };
auto tup2 = std::make_tuple( 11, x, 5.8, w );
std::cout << get<1>( tup2 ) << '\n';
}
Personally I prefer either direct access to a tuple's element using get or structured binding. I especially like structured binding since using auto means I don't have to know what the types are contained inside the tuple.