actually, I am new to C++ , although not new to programming. i have a vector of pairs ,

vector<pair<double, pair<int,int> >>

and i want to sort it in incresing order. for simplicity, i am using sort() inbulit function. does pair has ibuilt '<' operator ? or we have to override it ? if yes, then how will cmp will be declared ?

x,y are coordinates of a point and double is their polar angle. and i am doing this sorting so that after sorting the points are in the order which will make a polygon. thanks if u can help me. any help will be appreciated.

Edited 3 Years Ago by nitin1

From cplusplus.com:

The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= , so as to be able to compare pair objects of the same type directly:

Two pair objects are compared equal if the first elements in both objects compare equal to each other and both second elements also compare equal to each other - they all have to match.

In inequality comparisons (<, >), the first elements are compared first, and only if the inequality comparison is not true for them, the second elements are compared.

So, there you go :) As long as the things in the pair have operator< defined you're OK.

x,y are coordinates of a point and double is their polar angle. and i am doing this sorting so that after sorting the points are in the order which will make a polygon.

For a sort using the default operator< for std::pair<> to result in an ordering of the points of a polygon,

  • The polygon must a a convex polygon
  • The polar angle must be the polar angle based on a point which is within the convex polygon.

Note: You could also use std::vector< std::tuple< double, int, int > > instead of the vector of nested pairs.

You could also use std::vector< std::tuple< double, int, int > > instead of the vector of nested pairs.

It's a personal thing, but I'm not keen on tuple. std::tuple< double, int, int > isn't very meaningful. I'd much rather see a dedicated struct or class:

struct PolygonVertex
{
    int _x;
    int _y;
    double _polar_angle;
};

(I don't know if "vertex" is the right term, but you get the idea). Subsequent code is then more decipherable:

v[i]._y = 20;

versus

std::get< 2 >( v[i] ) = 20;

It's a personal thing...

Yes, it is.

A tuple would support genericity, it is a general-purpose metaprogrammable composite type.
For instance, a tuple can be used as it is with standard algorithms, as a key in an assiciative container.

A struct is an abstractor, a hider of information; a tuple is just an aggregator. For instance, this would be possible with tuples:

std::tuple< int, double > one { 23, 4.5 } ;
std::tuple< short, int > two = one ;
one = two ;

With structs, we would need to write special-purpose boilerplate code for all these. In general, a tuple is not a drop-in replacement for a struct, and vice versa. (Though boost fusion allows as to view a struct as a tuple when the occasion demands).

.
.

Subsequent code is then more decipherable:
v[i]._y = 20;
versus
std::get< 2 >( v[i] ) = 20;

Syntactic sugar is always possible; just that sometimes it requires a wee bit of knowledge/ingenuity.
(Libraries provide it as a matter of routine.)

#include <tuple>
#include <utility>
#include <iostream>

template < std::size_t POS > struct get_tuple_element
{
    template < typename T >
    auto operator() ( T&& t ) const -> decltype( std::get<POS>( std::declval<T>() ) )
    { return std::get<POS>( std::forward<T>(t) ) ; }
};

int main ()
{
    {
        using point = std::tuple<double,int,int> ; // ( polar_angle, x, y)
        constexpr auto polar_angle = get_tuple_element<0>() ;
        constexpr auto x = get_tuple_element<1>() ;
        constexpr auto y = get_tuple_element<2>() ;

        point pt { 0.48, 23, 45 } ;
        x(pt) = 79 ;
        y(pt) = 31 ;
        polar_angle(pt) = 0.36 ;

    }

    {
        // ( title, author, num_copies )
        using book = std::tuple< std::string, std::string, int > ; 
        constexpr get_tuple_element<0> title ;
        constexpr get_tuple_element<1> author ;
        constexpr get_tuple_element<2> num_copies ;

        book bk { "accelerated cpp", "koenig & moo", 8 } ;
        std::cout << title(bk) << " by " << author(bk) << " - " 
                  << num_copies(bk) << " copies\n" ;
        num_copies(bk) += 5 ;
        std::cout << num_copies(bk) << " copies\n" ;
    }
This article has been dead for over six months. Start a new discussion instead.