Tablica dwuwymiarowa - nietypowe sortowanie.
Ostatnio zmodyfikowano 2013-05-23 15:26
Elaine |
» 2013-05-22 22:16:30 zrób sobie specjalizacje std::less<std::pair<int, int> > |
Dobry pomysł, jeśli lubisz niezdefiniowane zachowanie. |
|
Maciek |
» 2013-05-23 01:49:41 #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 ?? |
|
Maciek |
» 2013-05-23 01:50:33 << removed >> |
|
Monika90 |
» 2013-05-23 09:29:09 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: 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. |
|
Maciek |
» 2013-05-23 12:07:28 jest jakiś paragraf w standardzie, że specjalizacja funktora std::less *może* powodować UB ?? |
|
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. |
|
|
1 « 2 » |