Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Tablica dwuwymiarowa - nietypowe sortowanie.

Ostatnio zmodyfikowano 2013-05-23 15:26
Autor Wiadomość
Elaine
» 2013-05-22 22:16:30
zrób sobie specjalizacje std::less<std::pair<int, int> >
Dobry pomysł, jeśli lubisz niezdefiniowane zachowanie.
P-83634
Maciek
» 2013-05-23 01:49:41
C/C++
#include <utility>
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstdlib>
#include <ctime>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>

namespace std
{
    template <>
    class less < pair < int, int > >
        : public binary_function < pair < int
        , int >
        , pair < int
        , int >
        , bool >
    {
    public:
        result_type operator ()( first_argument_type a, second_argument_type b ) const
        {
            if( a.first < b.first )
            {
                return true;
            }
            else if( a.first > b.first )
            {
                return false;
            }
            else
            {
                if( a.second < b.second )
                {
                    return true;
                }
                else if( a.second > b.second )
                {
                    return false;
                }
            }
            return true;
        }
    };
}

const std::pair < int, int >
pair_ii_generator( const int & min, const int max )
{
    return std::make_pair(( std::rand() %( max - min ) + min ),( std::rand() %( max - min ) + min ) );
}

int main()
{
    std::srand(( unsigned int )( std::time( NULL ) ) );
    std::vector < std::pair < int, int > > vec( 30 );
    std::generate( vec.begin(), vec.end(), boost::bind( pair_ii_generator, - 10, 10 ) );
    std::cout << "Table before sort:" << std::endl;
    std::for_each( vec.begin(), vec.end(), std::cout <<( & boost::lambda::_1 )->*( & std::pair < int, int >::first ) << " " <<
    ( & boost::lambda::_1 )->*( & std::pair < int, int >::second ) << "\n" );
    std::sort( vec.begin(), vec.end(), std::less < std::pair < int, int > >() );
    std::cout << "Table after sort:" << std::endl;
    std::for_each( vec.begin(), vec.end(), std::cout <<( & boost::lambda::_1 )->*( & std::pair < int, int >::first ) << " " <<
    ( & boost::lambda::_1 )->*( & std::pair < int, int >::second ) << "\n" );
    std::cin.get();
    return 0;
}

w którym miejscu mojego std::less<std::pair<int, int> > jest UB ??
P-83665
Maciek
» 2013-05-23 01:50:33
<< removed >>
P-83666
Monika90
» 2013-05-23 09:29:09
C/C++
if( a.first < b.first )
{
    return true;
}
else if( a.first > b.first )
{
    return false;
}
else
{
    if( a.second < b.second )
    {
        return true;
    }
    else if( a.second > b.second )
    {
        return false;
    }
}
return true;
Ale dlaczego tak? Kiedy można tak:
C/C++
return a.first < b.first ||( !( b.first < a.first ) && a.second < b.second );
A w ogóle nie jest to potrzebne, ponieważ std::pair ma operator <.

w którym miejscu mojego std::less<std::pair<int, int> > jest UB ??
Już samo dodanie jawnej specjalizacji std::less<std::pair<int, int>> to jest UB.
P-83667
Maciek
» 2013-05-23 12:07:28
jest jakiś paragraf w standardzie, że specjalizacja funktora std::less *może* powodować UB ??
P-83678
Elaine
» 2013-05-23 15:26:23
17.6.4.2.1/1:
The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
P-83699
1 « 2 »
Poprzednia strona Strona 2 z 2